In .Net any variable / object falls under one of the
types Value Type or Reference Type, by default structs are
value type and classes are reference type.
The following are some of the key difference
between value type and reference type variables / objects.
All Value Type
variables are implicitly derived from System.ValueType
All Reference Type
objects are implicitly derived from System.Object
Value
Type
variables have their own defined memory segment, hence copying from one value
type variable to another creates a new memory segment and the values of both
the variables are stored separately. Changing the value of one variable does
not affect the other.
Reference
Type
objects share the same memory segment, hence copying from one reference type object
to another just adds a pointer and both the objects point to the same base
memory segment, hence changing the value of one reference type object affects
the value of the other.
All Value Type
variables are stored in the stack
All Reference
Type objects are stored in the heap
Structs are Value Type
Classes are Reference Types
No comments:
Post a Comment