ValueType. Reference types add indirection. They point to another memory location. Value types do not—they are self-contained, often in the bytes on the evaluation stack.
ValueType details. Value types are used constantly in C# programs. Even a simple for-loop uses an int value type to iterate. All values are considered structs.
And Because it is the base class for values, you can refer to those values through a ValueType reference variable.
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());
}
}System.Int32
System.DateTime
System.Boolean
System.Int64
System.Int16
Notes, usage. A ValueType parameter a way to require that an argument is a value type. This is an additional constraint over an object parameter.
Detail This is the base class for all types. In this way it is similar to ValueType, but ValueType too is a subclass of object.
Base class. The ValueType class may be useful in some programs. It is important as a base class for actual value types. This is why it has no constructor.
Detail You should use the actual value types (such as int, long, short, DateTime) and not bother with ValueType itself.
Some research. A blittable type is compatible to an unmanaged type. Programs that use blittable types do not need to convert them before passing them to an unmanaged DLL.
Info In the book Expert .NET 2.0 IL Assembler we learn more about blittable types.
And In the .NET Framework some types, such as signed and unsigned integers, are blittable (page 371). They require no special conversion.
Detail This type is sometimes not blittable. Some unmanaged systems require a four-byte boolean.
Detail This is also sometimes not blittable. It may need to be converted to an ANSI (1-byte) character.
A summary. A variable stores a value. In programs, even a reference (pointer) is a value. It is simply a value that points to another value. The ValueType class is an important detail.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 26, 2022 (edit).