program to show difference between value type parameter and reference type parameter.

Prabhukiran345
Posted by Prabhukiran345 under C# category on | Points: 40 | Views : 430
using System;

namespace Example1
{
class Program
{
public static void value(int num)
{
num++;
}
public static void reference(ref int num)
{
num++;
}

static void Main(string[] args)
{
int num;
Console.Write("Enter a number:\t");
num = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\n\n\tValue Type");
Console.WriteLine("----------------");
Console.Write("\nPrevious Value:\t{0}", num);
Program.value(num);
Console.Write("\nCurrent Value:\t{0}", num);

Console.WriteLine("\n\n\n----------------");
Console.WriteLine("\tReference Type");
Console.WriteLine("--------------------");
Console.Write("\nPrevious Value:\t{0}", num);
Program.reference(ref num);
Console.Write("\nCurrent Value:\t{0}", num);
Console.ReadLine();
}
}
}


OUTPUT:

Enter a number: 9


Value Type
---------------------------------

Previous Value: 9
Current Value: 9

---------------------------------
Reference Type
---------------------------------

Previous Value: 9
Current Value: 10

Comments or Responses

Login to post response