Sometimes a user search with these kind of questions :-
what are the basics differences between value type and reference type in C#
reference type vs value type in C#
value type and reference type in C#
Solution :-
Definition : Value types are those which hold both data and memory on the same location. Reference types are those which has a pointer which points to the memory location. Something like this...
using System;
namespace demo
{
class Program
{
public static void valueType(int number)
{
number++; // or number=number+1
}
public static void referenceType(ref int number)
{
number++; // or number=number+1
}
static void Main(string[] args)
{
int number;
Console.Write("Enter any number : ");
number = Convert.ToInt32(Console.ReadLine()); // read user input number
Console.WriteLine("\nValue Type");
Console.Write("\nPrevious Value : {0}", number);
Program.valueType(number); // call static method of Program class
Console.Write("\nCurrent Value : {0}", number);
Console.WriteLine("\nReference Type");
Console.Write("\nPrevious Value : {0}", number);
Program.referenceType(ref number); // call static method of Program class
Console.Write("\nCurrent Value : {0}", number);
Console.ReadLine();
}
}
}
That’s it!!…..Happy Programming...
No comments:
Post a Comment