System.Runtime.InteropServices.MarshalTests.StringToCoTaskMemAuto C# (CSharp) Method

StringToCoTaskMemAuto() private method

private StringToCoTaskMemAuto ( ) : void
return void
        public static void StringToCoTaskMemAuto()
        {
            String s = null;

            // passing null string should return 0
            Assert.Equal(0, (long)Marshal.StringToCoTaskMemAuto(s));        
            
            s = "Hello World";
            IntPtr ptr = Marshal.StringToCoTaskMemAuto(s);

            // make sure the native memory is correctly laid out
            for (int i=0; i < s.Length; i++)
            {
                char c = (char)Marshal.ReadInt16(IntPtr.Add(ptr, i<<1));
                Assert.Equal(s[i], c);    
            }

            // make sure if we convert back to string we get the same value
            String s2 = Marshal.PtrToStringAuto(ptr);
            Assert.Equal(s, s2);

            // free the native memory
            Marshal.FreeCoTaskMem(ptr);  
        }