Value types are derived implicitly from the System.ValueType base type. Value types are variables that contain their data directly instead of containing a reference to the data stored in memory. Instances of value types are stored in memory called the stack, where the runtime can create, read, update, and remove them quickly.
Value Type
Value types are derived implicitly from the System.ValueType base type. Value types are variables that contain their data directly instead of containing a reference to the data stored in memory. Instances of value types are stored in memory called the stack, where the runtime can create, read, update, and remove them quickly.
There are three types.
1. Built-in types
2. User-defined types
3. Enumerations
Built-in types
All built-in numeric types are value types. A numeric type based on the size of the values.
|
Type
(VB/C#)
|
.NET Framework
|
Bytes
|
Range
|
Use
for
|
|
SByte/sbyte
|
System.SByte
|
1
|
–128
to 127
|
Signed bytes
|
|
Byte/byte
|
System.Byte
|
1
|
0
to 255
|
Unsigned bytes
|
|
Short/short
|
System.Int16
|
2
|
–32768
to 32767
|
Interoperation and
other specialized uses
|
|
Integer/int
|
System.Int32
|
4
|
–2147483648
to 2147483647
|
Whole numbers
|
|
(UInteger/uint)
|
System.UInt32
|
4
|
0
to 4294967295
|
Positive whole numbers
|
|
Long/long
|
System.Int64
|
8
|
–223372036854775808 to
9223372036854775807
|
Large whole numbers
|
|
Single/float
|
System.Single
|
4
|
–3.402823E+38
to 3.402823E+38
|
Floating point numbers
|
|
Double/double
|
System.Double
|
8
|
–1.79769313486232E+308
to
1.79769313486232E+308
|
large floating point
numbers
|
|
Decimal/decimal
|
System.Decimal
|
16
|
–79228162514264337593543950335
to
79228162514264337593543950335
|
Financial and
scientific calculations
|
|
Other Value Types
|
|
Char/char
|
System.Char
|
2
|
N/A
|
Single characters
|
|
Boolean/bool
|
System.Boolean
|
4
|
N/A
|
True/False
|
|
Date/date
|
System.DateTime
|
8
|
1/1/0001 12:00:00 AM
To
12/31/9999 11:59:59 PM
|
Date Time
|
|
|
System.Guid
|
|
|
globally
unique identifier, 128-bit
|
|
|
|
|
|
|
|
How to Declare Value Types
Value types are an implicit constructor declaring them instantiates the type automatically, you don’t have to include the new keyword. The constructor assigns a default value (null or 0), but you should always explicitly initialize the variable within the declaration.
Eg:-
//C#
bool b=false;
'VB.Net
Dim b As Boolean = False
How to Create User-Defined Types
User-defined types are also called structures or struct. Value type local instances are allocated on the stack and they contain their data directly. In other ways, structures nearly behave like classes.
structures by using the Structure keyword in VB. And the struct keyword in C#.
For Eg: -
class Test
{
struct Emp
{
public int ID;
public double Salary;
};
static void Main()
{
Simple s;
s.ID = 1;
s. Salary = 15520.50;
}
}
How to Create Enumerations
Enumerations (enum) are a value type with a set of related symbols that have fixed values. Use enumerations to provide a list of choices for developers using your class. The purpose of enumerations is to simplify coding and improve code readability by meaningful name instead of simple numeric values.
Eg :
VB.NET
Enum Days as Integer
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
End Enum
C#
enum Days : int { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };