Loading ...

Value Types Vs Reference Types

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  getting started / general asp.net   » Value Types Vs Reference Types

Value Types Vs Reference Types

Posts under the topic: Value Types Vs Reference Types

Posted: 10/17/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

Hello Folks,

Value Type - Value type variables directly contains values and stored on the stack.

Reference Type - Reference type do not hold the actual data for the object.When a reference type instance is created store the address of their data on the stack and actual data stored on the heap.

Example - 

Int a=5;

String B="Mohit";

MEMORY diagram -

Copy -

Int C = A;

String B = D;

 

Its mean - Changing the value of one value type(A) does not affect the value of another value type(C). but in case of reference type if I will change value of B, it would effect on value of D also.

Am I right?

Please tell me if I am right or wrong:)

Thanks in advance.


Posted: 10/17/2010

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490
  Answered

In your case it won't affect because the answer is that strings are immutable. They will still have different values.


Posted: 10/17/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

 

raghav_khunger said:

user="Raghav Khunger"]

In your case it won't affect because the answer is that strings are immutable. They will still have different values.

 

 

 

What about my memory Diagram image sir?

Like I want to know in normal case Value type variables directly contains values and stored on the stack and when I copy, it will create a new memory allocation.When a reference type instance is created store the address of their data on the stack and actual data stored on the heap.

Is my statement right in normal case??


Page 1 of 1 (3 items)