What are Value Types in .NET?Each variable
you use in your .NET program will be of a specific type. Most commonly
used type in .NET is the value types.
All your primitive data types like integer are part of value types. Value types are allocated in stack and not heap. When you copy one value type to another, both will have independent copy of the data. Different value types supported by .NET are mentioned below: " sbyte - 8 bit signed integer " byte - 8 bit unsigned integer " short - 16 bit signed integer " ushort - 16 bit unsigned integer " int - 32 bit signed integer " uint - 32 bit unsigned integer " long - 64 bit signed integer " ulong - 64 bit unsigned integer " char - 16 bit Unicode characters " float - single precision floating point number " double - double precision floating point number " bool - Boolean value true or false " decimal - 28 digit decimal number " Enumerations " Structures Here is an
example demonstrating usage of few value types and performing operations
between value types: Value types
are similar to primitive types in other languages. But there is an interesting
feature in .NET that differentiates it from others. In .NET, value types
are also inherited from System.Object. Hence they also have the characteristics
of Object. Here is an example to demonstrate that: The toString()
method belongs to an Object. But it works fine with value types. This
is because value types are also inherited from objects. However value
types are primitive in nature thereby avoiding the performance overhead
in objects. Any time, value types can be assigned to and from objects
by performing boxing and unboxing respectively.
|