1

I know that String is a reference type. My question is why it behaves like value type ?

As per my understanding reference type store memory location of heap in stack. So if string is reference type then it should be same behaviour like any reference type object like class.

For checking I have written below code:

static String justtest = new String('h', 1);
    public static void Method1(String source) 
    {
        source= source.ToUpper();
        Console.WriteLine(source);
    }

    public static void Method2(ReferenceClass me)
    {
        me.MyProperty = 100 + " Test";
    }

    static void Main(string[] args)
    {        
        Method1(justtest);
        Console.WriteLine("Value of String: {0}", justtest);            

        ReferenceClass obj1 = new ReferenceClass();
        obj1.MyProperty = "50";
        Method2(obj1);
        Console.WriteLine("Value of property: {0}", obj1.MyProperty);

        Console.ReadKey();
    }

    public class ReferenceClass {
        public string MyProperty;
    }

So it displays result as below:

    Value of String: h
    Value of property: 100 Test

Here property value is updated but not updated string value. So chcking the reference is same or not for string I am passing to method I have written below code:

    static String justtest = new String('h', 1);
    public static void Method1(String source) 
    {
        bool isequalref = ReferenceEquals(source, justtest);
        source= source.ToUpper();
        bool referenceNotEqual = ReferenceEquals(source, justtest);            
    }

    public static void Method2(ReferenceClass me)
    {
        me.MyProperty = 100 + " Test";
    }

    static void Main(string[] args)
    {        
        Method1(justtest);
        Console.WriteLine("Value of String: {0}", justtest);            

        ReferenceClass obj1 = new ReferenceClass();
        obj1.MyProperty = "50";
        Method2(obj1);
        Console.WriteLine("Value of property: {0}", obj1.MyProperty);

        Console.ReadKey();
    }

    public class ReferenceClass {
        public string MyProperty;
    }

output is same but at time of debugging I found value of isequalref is true while referenceNotEqual value as false.

So I am little bit confuse that is it because of string is immutable object so when we try to update it creates new object and refernce to other location. Is it so ? If yes then why it's not have updated value in main method ? If we update string in same method (main) then also it create new object but string will have new value. Like..

    static void Main(string[] args)
    {        
        Method1(justtest);
        Console.WriteLine("Value of String: {0}", justtest);            
        justtest = "Hello World";
        Console.WriteLine("Value of String: {0}", justtest);            

        ReferenceClass obj1 = new ReferenceClass();
        obj1.MyProperty = "50";
        Method2(obj1);
        Console.WriteLine("Value of property: {0}", obj1.MyProperty);

        Console.ReadKey();
    }

So could anyone help me to understand that what's reason beind string value not updated in external method when we pass as parameter though string is reference type.

13i-iavi
  • 21
  • 5
  • see ur [answer](http://stackoverflow.com/questions/636932/in-c-why-is-string-a-reference-type-that-behaves-like-a-value-type) here – Jins Peter Apr 27 '17 at 05:23
  • @samaxe - my question is different then In C#, why is String a reference type that behaves like a value type?: [link](http://stackoverflow.com/questions/636932/in-c-why-is-string-a-reference-type-that-behaves-like-a-value-type). Updated question heading. – 13i-iavi Apr 27 '17 at 05:44
  • It doesn't. It behaves like a reference type, but references are passed by value. Please read http://jonskeet.uk/csharp/parameters.html. – Jon Skeet Apr 27 '17 at 05:59

0 Answers0