Function with reference and value type parameter

Ranjeet_8
Posted by Ranjeet_8 under C# category on | Points: 40 | Views : 1751

class Program
{
static int age = 30;
static void Main(string[] args)
{
// Value Type
Console.WriteLine("Your birth year is {0}", ValBirthYear(age));
Console.WriteLine("if your age is {0}", age);
// Reference Type
Console.WriteLine("Your birth year is {0}", RefBirthYear(ref age));
Console.WriteLine("if your age is {0}", age);
Console.ReadKey();
}
//Function with value type parameter
private static string ValBirthYear( int Vage)
{
int year = DateTime.Now.Year - Vage;
Vage = year; //Assigning the calculated value to the parameter
return Convert.ToString(year);
}
//Function with reference type parameter. Notice the use of ref keyword.
private static string RefBirthYear(ref int Rage)
{
int year = DateTime.Now.Year - Rage;
Rage = year; //Assigning the calculated value to the parameter
return Convert.ToString(Rage);
}
}

Output (Value Type)
Your birth year is 1980
If your age is 30
Output (Ref Type)
Your birth year is 1980
If your age is 1980

Comments or Responses

Login to post response