Value Type Vs Reference Type:
We will differentiate between the Value Type and Reference Type in C# Programming:
Value Type:
–Variables that are based on value types
directly contain values. Assigning one value type variable to another copies
the contained value. This differs from the assignment of reference type
variables, which copies a reference to the object but not the object itself.
–All value types are derived implicitly
from the System.ValueType.
–Unlike with reference types, you cannot
derive a new type from a value type. However, like reference types, structs
can implement interfaces.
–Unlike reference types, a value type
cannot contain the null value. However, the nullable
types feature does allow for value types to be assigned to null.
–Each value type has an implicit default constructor that
initializes the default value of that type.
Examples of Value Type:
–The value types consist of two main
categories:
• Structs
–Numeric types
–Integral types
–Floating-point types
–decimal
–bool
–User defined structs.
• Enumerations
Reference Type:
– Variables of reference types store
references to the actual data.
• class
• interface
• Delegate
• Copies a
reference to the object but not the object itself.
• Default reference is null
Difference Between Argument and Parameter:
public void abc(int x) // 'x' is parameter
{
}
int a = 50;
abc(a); // 'a' is argument here
Passing Arguments by Value Type:
Passing the arguments \ Functions \ Methods by Value or By Reference is the Main Headache of Most of the Students. In Value Type, When you pass an argument, a separate copy is made.
For Example:
Main()
{
int a = 10;
method(a); //Here we are Passing an argument 'a' by Value Type , also Learn the Difference between argument and parameter that 'a' is a argument here
Console.WriteLine(a); // Output will be 10
}
public void method(int x) // Parameter of Value Type, Learn 'x' is Parameter here not an Argument
{
Console.WriteLine(x); //Output will be 10
x=20;
Console.WriteLine(x); // Output will be 20
}
In above example Value of 'x' has been changed but value of 'a' will remain unchanged because in value type 'x' has its own separate memory location.
At last, x = 20, and a = 20.
Passing Arguments by Reference Type:
When You pass an argument by Reference Type, separate copy is not made and the memory location of the argument is assigned to the Parameter, So when the value of Parameter is Changed, The value of argument also change automatically.
Main()
{
int a = 10;
method(ref a); //Here we are Passing an argument 'a' by reference Type , also Learn the Difference between argument and parameter that 'a' is a argument here
Console.WriteLine(a); // Output will be 20
}
public void method(int x) // Parameter of Reference Type, Learn 'x' is a Parameter here not an argument
{
Console.WriteLine(x); //Output will be 10
x=20;
Console.WriteLine(x); // Output will be 20
}
Another Example:
class Program
{
public void
Change(ref int k)
{
k = 100;
}
public void NoChange(int k)
{
k = 999;
}
public static void
Main()
{
Program p = new Program();
int a = 10;
p.Change(ref a);
System.Console.WriteLine("a
= {0}",
a); // Output will 999
p.NoChange(a);
System.Console.WriteLine("a
= {0}",
a); // Output will be 10
}
}
Thank you for Learning, Like this if you understood, It HELPS a lot