This C# Program Demonstrates the Difference between Value Type Parameter and Reference Type Parameter.The increment of an variable id done by both value type parameter and reference type parameter.
Here is source code of the C# Program to Demonstrate the Difference between Value Type Parameter and Reference Type Parameter. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
/*
* C# Program to Demonstrate the Difference between
* Value Type Parameter and Reference Type Parameter
*/
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\tValue Type:");
Console.Write("\nPrevious Value:\t{0}", num);
Program.value(num);
Console.Write("\nCurrent Value:\t{0}", num);
Console.WriteLine("\n\tReference Type");
Console.Write("\nPrevious Value:\t{0}", num);
Program.reference(ref num);
Console.Write("\nCurrent Value:\t{0}", num);
Console.ReadLine();
}
}
}
Here is the output of the C# Program:
Enter a Number : 10 Value Type : Previous Value : 10 Current Value :10 Reference Type : Previous Value : 10 Current Value :11
Sanfoundry Global Education & Learning Series – 1000 C# Programs.
advertisement
If you wish to look at all C# Programming examples, go to 1000 C# Programs.
Related Posts:
- Check Computer Science Books
- Practice MCA MCQs
- Check C# Books
- Apply for C# Internship
- Check MCA Books