Jump to content

  • Log In with Google      Sign In   
  • Create Account


C# Reference Types


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Orangeatang   Members   -  Reputation: 1248

Like
0Likes
Like

Posted 19 February 2013 - 07:16 AM

Hi there,

 

I've got a query about C# and returning reference types... if I have a class like :

class TestClass
{
  private int aNumber                 = 3;
  private string aString              = "hello";

  public int Number
  {
     get { return aNumber; }
     set { aNumber = value; }
  }

  public String TheString
  {
    get { return aString; }
  }
}

And I do the following :

TestClass testClass = new TestClass();

String aString = testClass.TheString;
System.Console.WriteLine( "String Value " + aString );
aString = "there";
System.Console.WriteLine( "Class String Value " + testClass.TheString );

The string value stored in the TestClass instance (testClass) isn't modified by :

 

   aString = "there"; 

 

I thought that strings were a reference type... must be missing something here smile.png



Sponsor:

#2 Telastyn   Crossbones+   -  Reputation: 3692

Like
1Likes
Like

Posted 19 February 2013 - 07:41 AM

They are a reference type, a reference to the (shared) value - not a reference to the variable.



#3 Orangeatang   Members   -  Reputation: 1248

Like
0Likes
Like

Posted 19 February 2013 - 08:14 AM

They are a reference type, a reference to the (shared) value - not a reference to the variable.

Ah gotcha - so the local variable initially references the data in the class's variable, but the assignment operation changes the local variable's reference to another bit of data...



#4 ColinDuquesnoy   Members   -  Reputation: 964

Like
0Likes
Like

Posted 19 February 2013 - 08:33 AM

In c#, a String is immutable.



#5 phil_t   Crossbones+   -  Reputation: 2484

Like
0Likes
Like

Posted 19 February 2013 - 01:23 PM

In c#, a String is immutable.

 

Regardless, in this case he is assigning a completely new value to a local variable - at this point it has nothing to do with TestClass.aString (i.e. this would be true even if he were using a class other than string - it has nothing to do with the fact that strings are immutable).



#6 ChaosEngine   Crossbones+   -  Reputation: 1882

Like
0Likes
Like

Posted 19 February 2013 - 02:32 PM

In c#, a String is immutable.

 
Regardless, in this case he is assigning a completely new value to a local variable - at this point it has nothing to do with TestClass.aString (i.e. this would be true even if he were using a class other than string - it has nothing to do with the fact that strings are immutable).

 
True, but the fact that Strings are immutable is important here.
 
Consider the following code:

public class RefClass
{
    public int IntProperty { get; private set; }

    public void DoStuff()
    {
        IntProperty++;
    }
}

public class ContainingClass
{
    private readonly RefClass _ref = new RefClass();

    public RefClass Ref
    {
        get { return _ref; }
    }
}

It's possible to do something like

ContainingClass c = new ContainingClass();
Debug.Assert(c.Ref.IntProperty == 0);
RefClass r = c.Ref;
r.DoStuff();
Debug.Assert(c.Ref.IntProperty == 1);

With a string, that wouldn't be possible because there are no operations that mutate a string.


if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

#7 phil_t   Crossbones+   -  Reputation: 2484

Like
0Likes
Like

Posted 19 February 2013 - 04:05 PM

You're right, but I was specifically talking about assignment. In the original case, if you substitute string with StringBuilder (and have TestClass put "hello" in it by default), then by reassigning:

 

TestClass testClass = new TestClass();
StringBuilder aString = testClass.TheString;
System.Console.WriteLine( "String Value " + aString ); // "hello"
aString = new StringBuilder("there");
System.Console.WriteLine( "Class String Value " + testClass.TheString ); // Still "hello"

 

 

but, as in your "DoStuff" example:

 


TestClass testClass = new TestClass();
StringBuilder aString = testClass.TheString;
System.Console.WriteLine( "String Value " + aString ); // "hello"
aString.Clear();
aString.Append("there");
System.Console.WriteLine( "Class String Value " + testClass.TheString ); // "there"





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS