System.ValueType
Represents value types, which are the base class for all value types, including enumerations and structures.
Syntax
public abstract class ValueType
Remarks
Value types are a fundamental concept in the .NET Framework. Unlike reference types, which are stored on the heap and accessed through references, value types are stored directly in the memory location where they are declared. This means that when you assign a value type to another variable, a copy of the value is made.
All primitive data types in C#, such as int
, float
, bool
, and char
, are value types. User-defined types declared with the struct
keyword are also value types. Enumerations are also considered value types.
The ValueType
class overrides the default implementations of Equals
, GetHashCode
, and ToString
inherited from Object
. These overrides provide default behavior for value types, typically based on member-wise comparison and representation.
While ValueType
is an abstract class, you will rarely, if ever, directly inherit from it. Instead, you will use primitive types or define your own structures.
Methods
Name | Description |
---|---|
Equals | Determines whether the specified object is equal to the current object. |
GetHashCode | Serves as the default hash function. |
ToString | Returns a string that represents the current object. |
Equals Method
public override bool Equals(object obj)
Returns
true
if the specified object is equal to the current object; otherwise, false
.
Remarks
The default implementation of the Equals
method determines equality by performing a member-wise comparison of the two objects. For value types, this means that two objects are considered equal if they have the same type and all their corresponding fields are equal.
GetHashCode Method
public override int GetHashCode()
Returns
A hash code for the current object.
Remarks
The default implementation of GetHashCode
returns a hash code based on the contents of the value type's fields.
ToString Method
public override string ToString()
Returns
A string that represents the current object.
Remarks
The default implementation of ToString
returns the fully qualified name of the type. For example, calling ToString
on an int
variable named myInt
might return "System.Int32".
See Also
- System.Object
- Structures (Conceptual)
- System.Enum