• C# supports two kinds of types: value types and reference types.

    • Value types include simple types (e.g., char, int, and float), enum types, and struct types.

      • Value types differ from reference types in that variables of the value types directly contain their data, whereas variables of the reference types store references to objects.

    • Reference types include class types, interface types, delegate types, and array types.

      • 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. 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. Bottom line value types remain unchanged where as reference type can be overridden.

Passing values among same types :
int value1 = 0;
int value2 = value1;
// assign value2, that will not effect value1.
value2 = 123;

 

Ref_Type

compare ref1 = new compare();
//casting one object to another
compare ref2 = ref1;
ref2.x = 1234;

using System;
// csc value_or_ref_Type.cs
class compare
{
public int x = 0;
}
class Test
{
static void Main() {
int value1 = 0;
int value2 = value1;
//rea assign value2, that will not effect value1.
value2 = 123;
compare ref1 = new compare();
//casting one object to another
compare ref2 = ref1;
ref2.x = 1234;
Console.WriteLine("Values: {0}, {1}", value1, value2);


Console.WriteLine("Refs: " + ref1.x + " and " +ref2.x);
}
}

 
Heap or stack

We  Know that there are two type of variable 1) Value Type and  2) reference Type. This two  allocated in differnt space in memory  and  algorithm used for allocating  this two  variable are different. one is called Stack Value) and another is Heap (Reference) type.