0

I had read an article http://www.albahari.com/valuevsreftypes.aspx

As per this, int is a value type and form is an example of reference type.

Point myPoint = new Point (0, 0);      // a new value-type variable
Form myForm = new Form();              // a new reference-type variable
string mystringval="test";
Test (myPoint, myForm);                // Test is a method defined below

void Test (Point p, Form f)
{
      p.X = 100;                       // No effect on MyPoint since p is a copy
      f.Text = "Hello, World!";        // This will change myForm’s caption since
                                       // myForm and f point to the same object
      f = null;                        // No effect on myForm
}

So instead of form variable myform, if I pass a string value to the function test, will it also change the original value which I already declared outside?

Also what is the benefit of keeping the string as reference type, if, anyhow the value will be saved in stack and only the reference will be stored in heap?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Tom Cruise
  • 1,395
  • 11
  • 30
  • 58
  • http://stackoverflow.com/questions/636932/in-c-why-is-string-a-reference-type-that-behaves-like-a-value-type – Keith Rousseau Sep 25 '15 at 23:46
  • @GrantWinney, if strings are immutable, why its not updated with the last value provided? why should i give ref to a reference type? – Tom Cruise Sep 25 '15 at 23:55

1 Answers1

4

if i pass a string value to the function test, will it also change the original value which i already declared outside?

No, neither did the value of myForm change, the myForm variable still points/refers to the same Form object (the object created via new Form()).

In your Test method, when you called:

f.Text = "Hello, World!";

You did not change the variable f, you changed f.Text which is a different.

In other words, you are changing a property on the object that both f and myForm refer to. But still f and myForm refer to the same original object.

Note that when you changed f to null, the variable myForm did not change. Now, f refers to nothing, but the myForm variable still refers to the original object.

If you want to change the original myForm variable (make it point/refer to another Form object) or mystringval (make it refer to another string) then pass them to the test method by reference like this:

void Test (Point p, ref Form f)
{
    ...
}

void Test (Point p, ref string s)
{
    ...
}
Yacoub Massad
  • 27,509
  • 2
  • 36
  • 62
  • Thank you ..i understood your point. So the string is categorized as reference type as it may contain huge data which cannot handled by stack. am i right? – Tom Cruise Sep 26 '15 at 01:09
  • If it so, the value of string will be stored in heap? what is the benefit of keeping the string in heap? – Tom Cruise Sep 26 '15 at 01:09
  • I believe it's not a matter of choice. Because of the nature of strings size, it needs to be in the heap. After all in c, string is just a pointer to memory where the end is marked by a /0. Could be wrong here since I'm rather new to c#. – Khanal Sep 26 '15 at 01:24
  • 1
    @user833985 - I think you've got a few misconceptions about the stack and the heap.You should ignore the distinction as it doesn't really matter to the behavour of the objects as to where they are stored. What you should focus on are these two concepts - "value vs reference types" & "mutable vs immutable types". – Enigmativity Sep 26 '15 at 01:41
  • Correct. The heap vs. the stack are implementation details. The CLR certainly doesn't use the size of objects as a determining factor in where the object is stored. And even if it did, it'd be required to make sure that the behavior of objects didn't change if they crossed a size threshold and ended up being stored in a different data structure. That would lead to severely non-deterministic programs. – siride Sep 26 '15 at 05:50
  • [This question](http://stackoverflow.com/questions/3655984/why-is-string-a-reference-type) explains why string is categorized as a reference type. – Yacoub Massad Sep 26 '15 at 10:42