There are diffrent behavior for the diffrent type of variables. if we talk about value types they are consist about the values only. the don't share memory locations. so every value type have there own values and they can play with it as they want. on the other hand refrence type variables are diffrent in nature they work with sharing mode so if i create an object then the value of this object is being shared by their users. I recomand you to have a look for below video for more clearification.
So if you pass a value type variable to a method the copy of values is being assigned to the using variable. so if you want to use the same variable. then you can have ref keywork for refranceing the value of the same object. on the other side object / refrence type variables are not needed to be ref keyword as they are already refranced by default nature.
Try below code tu understand the behavior.
class Program
{
static object reftypedata = "I am Testing. ";
static int valuetypeData = 10;
static void Main(string[] args)
{
CalcValue(valuetypeData);
CalcValuewithRef(ref valuetypeData);
Calcobject(reftypedata);
Console.ReadKey();
}
public static void CalcValue(int i)
{
Console.WriteLine("Value Type.....");
Console.WriteLine("before Processing i={0}", i);
i = 100;
Console.WriteLine("After Processing i={0}", i);
Console.WriteLine("After Processing valueTypeData = {0}", valuetypeData);
}
public static void CalcValuewithRef(ref int i)
{
Console.WriteLine("Value with Ref Type.....");
Console.WriteLine("before Processing i={0}", i);
i = 100;
Console.WriteLine("After Processing i={0}", i);
Console.WriteLine("After Processing valueTypeData = {0}", valuetypeData);
}
public static void Calcobject(object value)
{
Console.WriteLine("Reference Type.....");
Console.WriteLine("before Processing value={0}", value);
value = "Default Value Has been Changed.";
Console.WriteLine("After Processing value={0}", value);
Console.WriteLine("After Processing reftypedata={0}", reftypedata);
}
}
Output:
Value Type.....
before Processing i=10
After Processing i=100
After Processing valueTypeData = 10
Value with Ref Type.....
before Processing i=10
After Processing i=100
After Processing valueTypeData = 100
Reference Type.....
before Processing value=I am Testing.
After Processing value=Default Value Has been Changed.
After Processing reftypedata=I am Testing.
you also can reffer to this youtube video.