CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

C# – Value Types and Reference Types

C# types majorly of two kinds, which are; Value Types and Reference Types.

Value Types

When you define a variable of this type; the variable holds the data directly. And each variable occupies it’s own memory to hold it’s value. That means, each variable has it’s own copy of the data; and if you change the data of one variable, it can’t affect to other variable. For example, if you define a variable;

int val = 100;

The compiler allocates a memory for the Value Type variable val and assign a value 100 to it. It allocates the memory in the stack; local memory of the running program.

How do we know which type is Value Type?

Basically Value Type includes; simple types, enum types and struct types. Simple or primitive types means; int, float, char etc,.

Reference Types

Unlike Value Type variables; Reference Type variables holds the address of the memory where the actual data is stored. These are like pointers in C and C++. Whenever you declare a variable of reference type; the compiler assigns the address of the data instead of the data itself.

How do we know which type is Reference Type?

The Reference Type includes; class types, interface types, arrays and delegates.

Memory for reference types are allocated in heap memory.

(Raju)

C# – Value Types and Reference Types

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top