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:
class sampleClass {
public static void Main() {
int intVar1 = 100;
int intVar2 = intVar1;
Console.WriteLine("intVar1 = {0}, intVar2 = {1}", intVar1, intVar2);
double doubleVar1 = intVar1 + 1.3;
intVar2 = (int) doubleVar1;
Console.WriteLine("doubleVar1= {0}, intVar2 = {1}", doubleVar1, intVar2);
bool boolValue = (intVar2 % 2 == 0);
char charValue = 'A';
Console.WriteLine("boolValue= + boolValue +" ,charValue = " + charValue);
}
}
Output of this code will be:
intVar1 = 100, intVar2 = 100
doubleVar1 = 101.3, intVar2 = 101
boolValue = false , charValue = A

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:
class sampleClass {
public static void Main() {
int intVar1 = 100;
Console.WriteLine("intVar1 = " + intVar1.toString();
}
}
Output of this code will be:
intVar1 = 100

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.

| How do you prevent a class from overriding in .NET? | How are classes related to objects in .NET Application | How are Delegates different from Events in .NET? | How are system exceptions different from application exceptions in .NET? | How are Value Types different from Reference Types in .NET? | How can a finalize method be suppressed in .NET? | How can you call Stored Procedure in ADO.NET? | How can you force Dispose method to be called automatically in .NET? | How do you call a Base Class Constructor from Derived Class Constructor in .NET? | How do you connect your VB.NET application to SQL Server? | How do you implement Cloning in .NET? | How do you implement Façade Design Pattern in .NET? | How do you implement MVC Pattern in ASP.NET? | How do you install .NET Assembly in GAC? | How is shadowing different from overriding in .NET? | How to prevent a particular .NET DLL from being decompiled? | Illustrate Delay Signing Process of an Assembly in .NET? | What are Reference Types in .NET? | What are the advantages of C#? | What are the advantages of VB.NET? | What are the differences between Namespace and Assembly in .NET? | What are the similar features between class and structure in .NET? | What are Value Types in .NET? | What do you mean by mixed mode authentication in .NET? | What do you mean by Satellite Assembly in .NET? | What do you mean by shadowing in .NET? | What is CTS in .NET? | What is ILDASM in .NET? | What is Managed Code in .NET? | What is Manifest in .NET? | What is MSIL in .NET Framework? | What is the importance of finalize method in .NET? | What is the need for Visitor Pattern in C#? | What is the purpose of bindingRedirect tag in web.config file of .NET? | What is the purpose of CodeDom in .NET? | What is the purpose of dispose method in .NET? | What is the purpose of Ngen.exe in .NET? | What is the purpose of Strong Name in COM Components of .NET? | What is the purpose of virtual keyword in .NET? | What Object Oriented Principles can be incorporated in .NET Application? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.