0

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

Malaka
  • 1
  • 1
  • 2
    [existing answer](https://stackoverflow.com/a/76395388/479251) says it short and well. .NET is a "managed" framework, it means that memory allocations and de-allocations are "managed" by the .NET runtime, and you _shouldn't_ care. – Pac0 Jun 03 '23 at 09:28
  • in a sense, it's like dynamic data structures (List..). Internally, they are backed up by an array, which has a fixed size and contiguous in memory. If you add elements more than the size of the array, the List will accept that and just allocate a bigger array, which can or cannot result in this array starting elsewhere in memory. A string is backed up by an array of char. – Pac0 Jun 03 '23 at 09:31
  • 2
    The ToString call on a StringBuilder may create a new string as *copy* of its buffer - so why would two results have the same address? – Hans Kesting Jun 03 '23 at 10:24
  • If you checked the memory address of your `str2` variable itself, instead of the result of calling `.ToString()`, you would find it has the same memory address because it's the same instance of StringBuilder. But `ToString()` returns a `string`, which is immutable regardless of how it was built. It is impossible for two `string`s to have different values but have the same address. – StriplingWarrior Jun 09 '23 at 04:48

1 Answers1

4

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.

SRNissen
  • 480
  • 9