Understanding Value Types in .NET


Value types (primitive type) ( simple type) are variable that contain their data directly.
They don’t contain reference to data stored elsewhere in memory.
Instances of values types are stored in an area of memory called stack where it is easy for the runtime to perform CRUD operation with minimal overhead.

Now what exactly is STACK based memory-
It is regions of memory where data is added or removed in a Last-In-First-Out manner.
each thread has a reserved region of memory referred to as its stack.
When a function executes, it may add some of its state data to the top of the stack; when the function exits it is responsible for removing that data from the stack. If a region of memory lies on the thread’s stack, that memory is said to have been allocated on the stack.
Thus stack allocation is very simple and typically faster.
Another advantage is that memory on the stack is automatically reclaimed when the function exits, which can be convenient for the programmer.
A disadvantage of stack based memory allocation is that a thread’s stack size can be as small as a few dozen kilobytes. Allocating more memory on the stack than is available can result in a crash .
Another disadvantage is that the memory stored on the stack is automatically deallocated when the function that created it returns, and thus the function must copy the data if they should be available to other parts of the program after it returns.

The are around 300 value types in .NET Framework. Most frequently used one are
System.SByte, Byte,Int16,Int32,UInt32,Inte64,Single,Double,decmal,char, boolean, datetime

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

Please share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.