简体   繁体   English

为什么在 C# 中使用 String Builder 关键字时 memory 地址不同?

[英]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.根据使用 String Builder 关键字手动调整字符串后的理论,两个 memory 地址必须相同,但我得到了错误的答案。 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.我认为通过使用 string Builder key wor 在两个场景中结果相同,但结果与预期不同。

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使用字符串关键字(不可变) str1 文本为:Interview Memory str1 的地址为:52066096 str1 修改后的文本为:InterviewHappy str1 内存地址为: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 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#? How does StringBuilder work internally in C#? 部分回答了您的问题?

The rest of the answer is:答案的rest是:

Stringbuilder may return the same address, if the manipulation you are doing can be done without performing new allocations.如果可以在不执行新分配的情况下完成您正在执行的操作,Stringbuilder可能会返回相同的地址。 It is not guaranteed to return the same address.保证返回相同的地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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