Overview

Represents the base class for all value types, which are constrained by the CLS. Value types are distinct from reference types, which are declared by classes. A value type cannot derive from any type other than System.Object, and it cannot derive from any other value type.

The System.ValueType class is the direct or indirect base class for all value types. It derives from System.Object.

public abstract class ValueType

Type Members

Methods

M: Equals(Object) Overrides Object.Equals(Object)
M: GetHashCode() Overrides Object.GetHashCode()
M: ToString() Overrides Object.ToString()

Inheritance

  • System.Object
  • System.ValueType
    • System.Boolean
    • System.Byte
    • System.Char
    • System.DateTime
    • System.Decimal
    • System.Double
    • System.Enum
    • System.Int16
    • System.Int32
    • System.Int64
    • System.IntPtr
    • System.SByte
    • System.Single
    • System.Structs
    • System.UInt16
    • System.UInt32
    • System.UInt64
    • System.UIntPtr

Remarks

Value types are types that are directly contained within their variable. They are not created on the heap and do not have the overhead of reference types (such as garbage collection). They are typically small and used to represent simple data.

When a value type is passed by value, a copy of the value is made. This means that changes to the copy do not affect the original value.

All value types implicitly inherit from System.ValueType. You cannot create a class that derives from System.ValueType. However, you can create a struct that derives from System.ValueType (which is the default for structs).

Examples

Implementing Value Type Behavior

While System.ValueType is an abstract base class, the concept is embodied by the struct keyword in C#.


// A simple struct demonstrating value type behavior
public struct Point
{
    public int X;
    public int Y;

    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }

    public override string ToString()
    {
        return $"({X}, {Y})";
    }
}

public class Example
{
    public static void Main(string[] args)
    {
        Point p1 = new Point(10, 20);
        Point p2 = p1; // p2 is a copy of p1

        p2.X = 100; // Modifying p2 does not affect p1

        Console.WriteLine($"p1: {p1}"); // Output: p1: (10, 20)
        Console.WriteLine($"p2: {p2}"); // Output: p2: (100, 20)
    }
}
                    

Equals(Object) Method

Determines whether the specified object is equal to the current object.


public override bool Equals(object obj)
                    

Parameters

obj: The object to compare with the current object.

Return Value

true if the specified object is equal to the current object; otherwise, false.

By default, the Equals method checks for value equality for value types. It compares the fields of the two objects.

GetHashCode() Method

Serves as the default hash function.


public override int GetHashCode()
                    

Return Value

A hash code for the current object.

The hash code is generated by combining the hash codes of the value type's fields.

ToString() Method

Returns a string that represents the current object.


public override string ToString()
                    

Return Value

A string that represents the current object.

The default implementation of ToString() for value types typically returns the fully qualified name of the type.