FAtechs is a Web Site where you can find a lot of things related to tech and Education. Our Posts are mostly about Programming, Information, Technology,Software, Reviews & Everything You need to Know. You can also find Books and Novels here. Your appreciation and criticism will be welcomed.

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

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