In .NET, the System.Object
class provides several methods that are inherited by all types. These methods are essential for basic object manipulation and interaction. Some of the key methods provided by System.Object
and inherited by all types in .NET include:
- Equals(object obj): Determines whether the current object is equal to another object. This method is often overridden in derived classes to provide custom equality comparison.
- GetHashCode(): Serves as a hash function for the object. It is typically used in hashing algorithms and data structures such as hash tables.
- GetType(): Gets the Type of the current instance. This method returns a
Type
object representing the runtime type of the current instance.
- ToString(): Returns a string that represents the current object. By default, it returns the fully qualified name of the object’s type.
- MemberwiseClone(): Creates a shallow copy of the current object. It creates a new object instance and copies the non-static fields of the current object to the new instance.
- Finalize(): Allows an object to clean up resources before it is reclaimed by garbage collection. However, it’s important to note that in modern .NET development, the use of
Finalize()
for resource cleanup is discouraged in favor of implementing the IDisposable
interface and using the Dispose()
method.
Additionally, the System.Object
class also provides several static methods like ReferenceEquals()
for reference comparison, and Equals()
for value comparison, but these are not necessarily exclusive to deriving classes/types as they can be used directly via the System.Object
class.
These methods provide foundational functionality for all types in .NET, and understanding how they work is crucial for effective object-oriented programming in the framework.