Need a Translation?

Value Types Vs. Reference Types

There are two kinds of types in C#: Value types and Reference types.

Value Types

  • Variables of value types directly contain their data in memory allocated on the stack.
  • Storage of contents of a variable in a stack increases efficiency, but the limited lifetime of value types makes them in-efficient for sharing data between different classes.
  • With value types, the variables each have their own copy of the data, and it is not possible for operations on one to affect the other (except in the case of ref and out parameter variables).
  • When a value type variable goes out of scope, the value is discarded from the stack.
  • C#’s value types are further divided into simple types, enum types, struct types (even if their members are reference types), and nullable types.
  • Value types are derived from System.ValueType.
  • When value types are passed By Value, the actual value is copied and there are no references involved. When value types are passed By Reference, the method argument is initialized with the address pointing back to the original value in the calling routine.

Example

class Program
{
static void Main(string[] args)
{
int iMarksOfRam = 65;
int iMarksOfTom = iMarksOfRam;
iMarksOfRam++; //
Console.WriteLine(iMarksOfTom); // it will display 65, iMarksOfRam++ won’t affect on iMarksOfTom
Console.WriteLine(iMarksOfRam); // it will display 66
Console.Read();
}
}

Reference Types

  • Variables of reference types store references to their data (known as objects) on the heap.
  • Declaration of reference types has a greater overhead, but they are accessible from other classes, which is a clear advantage.
  • With reference types, it is possible for two variables to reference the same object and thus possible for operations on one variable to affect the object referenced by the other variable.
  • When a reference type variable goes out of scope, the memory is not returned to the heap. Instead, it is reclaimed by the garbage collector when it determines that the variable is no longer needed.
  • C#’s reference types are further divided into class types, interface types, array types (even if their elements are value types), and delegate types.
  • Reference types are derived from System.Object.
  • 90% of the time, when reference types are passed By Value or By Reference, it is functionally the same. However, in horns of dilemma, pass reference types By Value and not By Reference!

Note: When a value type is passed by reference, it simply points back to the original value type. However, when a reference type is passed by value, it creates a copy of the reference (address) inside the method being invoked.

Example

class Program
{
static void Main(string[] args)
{
StringBuilder sReferenceTypeX = new StringBuilder("Hello ");
StringBuilder sReferenceTypeY = sReferenceTypeX;
sReferenceTypeX.Append("World");
Console.WriteLine(sReferenceTypeY); //it will display Hello World
Console.WriteLine(sReferenceTypeX); //it will display Hello World
Console.Read();
}
}

No comments:

Post a Comment