Array Collections File String Windows VB.NET Algorithm ASP.NET Cast Class Compression Convert Data Delegate Directive Enum Exception If Interface Keyword LINQ Loop Method .NET Number Regex Sort StringBuilder Struct Switch Time Value

Indirection measures the number of steps needed to load a value. Reference types add indirection. They point to another memory location. Value types do not. They are self-contained and represented completely by the bytes on the evaluation stack. Some value types include char, int, and bool. These all derive from the ValueType class.
Bool Type Byte Type Char Type Decimal Examples Double Type Float Notes Int Type Long Short TypeCompared
to reference types, value types are accessed faster, since there is no additional
indirection involved. Typical value types are complex numbers, geometric points,
or dates.
Miller & Ragsdale, p. 231
All value types implicitly inherit from the class System.ValueType,
which in turn inherits from class object.
Hejlsberg et al., p. 125
This simple program shows an int value type being used. Notice that no constructor is used on the int type; it is stored entirely within stack memory space. Also, when a number is added to the int, that memory location's value is changed. No new int is created as a result of the addition.
Program that demonstrates value type [C#]
using System;
class Program
{
static void Main()
{
int value = 10;
value += DateTime.Today.Day;
Console.WriteLine(value);
}
}
Output
15
Examples. There are many more examples of value types on this website. The DateTime type is an example of a struct. You can create enums that are value types with a backing store type of your choosing.
The ValueType class is a base class for all value types, which include ints, shorts, doubles, and also structs such as DateTime instances. We show how you can use ValueType to refer to these values, and also as a parameter type in methods.
Program that uses ValueType [C#]
using System;
class Program
{
static ValueType _valueType = false;
static void Main()
{
// You can refer to System.Int32 as a ValueType.
ValueType type1 = 5;
Console.WriteLine(type1.GetType());
// You can refer to System.DateTime as a ValueType.
ValueType type2 = DateTime.Now;
Console.WriteLine(type2.GetType());
// You can refer to System.Boolean as a ValueType.
Console.WriteLine(_valueType.GetType());
// Pass as parameter.
Method(long.MaxValue);
Method(short.MaxValue);
}
static void Method(ValueType type)
{
Console.WriteLine(type.GetType());
}
}
Output
System.Int32
System.DateTime
System.Boolean
System.Int64
System.Int16Explanation. ValueType is a class, which means it is not actually a value type itself. However, because it is the base class for values, you can refer to those values through a ValueType reference variable. Above, we showed a ValueType that is actually an int; a DateTime struct; and a bool field. Finally, we showed how you can use ValueType as a parameter.

Practical use. What is the practical use of a ValueType parameter, as shown in Method()? It is a way to require that all arguments sent to it are a value type. You can accept any argument with an object parameter, but occasionally it may be useful to constrain the argument to value type instances.
Object Type
The ValueType class, although useful in some narrow cases in actual programs, is most important as a base class for actual value types. This is why it does not have a constructor. You should simply use the actual value types (such as int, long, short, DateTime) and not bother with ValueType itself in most programs.
DateTime
We explored the concept of value types in the C# language. Further, we examined the ValueType class, which serves as an abstract base class for all value types. Most applications will focus on the value types themselves, such as ints, longs, shorts, or DateTimes.