1

This question came to me with this common code sample frecuently used to explain difference between value types and reference types:

class Rectangle
{
    public double Length { get; set; }
}

struct Point 
{
    public double X, Y;
}

Point p1 = new Point();
p1.X = 10;
p1.Y = 20;
Point p2 = p1;
p2.X = 100;
Console.WriteLine("p1.X = {0}", p1.X);

Rectangle rect1 = new Rectangle
{ Length = 10.0, Width = 20.0 };
Rectangle rect2 = rect1;
rect2.Length = 100.0;
Console.WriteLine("rect1.Length = {0}",rect1.Length);

In this case, the second Console.WriteLine statement will output: “rect1.Length = 100”

In this case class is reference type, struct is value type. How can I demostrate the same reference type behaviour using a string ?

Thanks in advance.

1
  • 5
    You can't. Strings are immutable. Commented Jan 15, 2014 at 22:14

5 Answers 5

5

You can't. Strings are immutable.. which means you can't change them directly. Any changes to a string are actually a new string being returned.

Therefore, this (which I assume you mean):

string one = "Hello";
string two = one;

two = "World";

Console.WriteLine(one);

..will print "Hello", because two is now an entirely new string and one remains as it was.

Sign up to request clarification or add additional context in comments.

2 Comments

so why do they use that code sample to demostrate reference type behaviour? What could be the difference between an int and string?
Strings are reference types.. but they appear to have value type semantics because they are immutable. "difference between int and string" .. ? In what context? (other than the fact they are completely different things)
1

only way to make a string a reference is to use a stringbuilder

class Program
{
    static void Main(string[] args)
    {

        string one = "Hello";
        string two = one;

        two = "World";

        Console.WriteLine(one);

        StringBuilder sbone = new StringBuilder( "Hello");
        StringBuilder sbtwo = sbone;

        sbtwo.Clear().Append("world");

        Console.WriteLine(sbone);


        Console.ReadKey();
    }
}

Comments

1

String is a reference type. It's an immutable (readonly) reference type. Because it's immutable, it will create a new instance each time you use an operator such as + or += etc. to modify it.

The fact that strings are read-only makes them behave similar to value types.

Comments

1

Like this (don't do it, though):

string a = "Hello";
string b = a;

unsafe
{
    fixed(char* r = a)
    {
        r[0] = 'a';
    }
    Console.WriteLine(a);
    Console.WriteLine(b);
}

Comments

1

What about create a 10MB long string, then set all elements of a very large array to be equal to it, using task manager you may be able to show that the ram usage has not gone up.

Note the process size after creating the array but before setting the string into the array elements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.