简体   繁体   中英

Why is there a difference in memory address when using String Builder keyword in C#?

according to the theory after manupaliting string using String Builder keyword both memory address must be same, but i get wrong answer. could anyone please explain this.

class Program
    {             
        static void Main(string[] args)
        {
            Console.WriteLine("using string keyword (immutable)");
            string str1 = "Interview";
            Console.WriteLine("str1 text is: "+ str1);
            unsafe
            {
                fixed (char* pointer = str1)
                {
                    Console.WriteLine("Memory address of str1 is: "+(int)pointer);
                }
                
            }

            str1 += "Happy";
            Console.WriteLine("str1 modified text is: "+str1);
            unsafe
            {
                fixed (char* pointer = str1)
                {
                    Console.WriteLine("str1 memeory address is: "+(int)pointer);
                }

            }
            Console.WriteLine("\n");
            Console.WriteLine("Using String Builder to (mutable string)");
            StringBuilder str2 = new StringBuilder();

            str2.Append ("Interview");
            Console.WriteLine("str2 text is:" + str2);
            unsafe
            {
                fixed (char* pointer = str2.ToString())
                {
                    Console.WriteLine("str2 memory address is: "+(int)pointer);
                }
            }
           
            str2.Append("Selection Process");
            Console.WriteLine("str2 modified text is: "+str2);
            unsafe
            {
                fixed (char* pointer = str2.ToString())
                {
                    Console.WriteLine("str2 aftermodified memory address is:"+(int)pointer);
                }
            }
            Console.ReadKey();
        }
    }

// result
using string keyword (immutable)
str1 text is: Interview
Memory address of str1 is: 52066096
str1 modified text is: InterviewHappy
str1 memeory address is: 52069440


Using String Builder to (mutable string)
str2 text is:Interview
str2 memory address is: 52070212
str2 modified text is: InterviewSelection Process
str2 aftermodified memory address is:52070828

i thought result was same on both sceniros by using string Builder key wor but result is different from expected.

result is

using string keyword (immutable) str1 text is: Interview Memory address of str1 is: 52066096 str1 modified text is: InterviewHappy str1 memeory address is: 52069440

Using String Builder to (mutable string) str2 text is:Interview str2 memory address is: 52070212 str2 modified text is: InterviewSelection Process str2 aftermodified memory address is:52070828

Your question is partially answered by How does StringBuilder work internally in C#?

The rest of the answer is:

Stringbuilder may return the same address, if the manipulation you are doing can be done without performing new allocations. It is not guaranteed to return the same address.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM