C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

Introduction To The C# Object Class

In C#, the Object class is the ultimate base class for all types, including value types and reference types. It is the root of the type hierarchy and is defined in the System namespace. Every class in C# implicitly inherits from the Object class if it doesn't explicitly inherit from another class.

In this tutorial, we'll cover the following topics related to the C# Object class:

  • Understanding the Object class
  • Methods of the Object class
  • Overriding Object class methods

Let's begin!

  • Understanding the Object class

As the root of the type hierarchy, the Object class provides a set of methods that are available to all types. This means that you can use these methods on any object, regardless of its type. Some of these methods are intended to be overridden by derived classes to provide custom behavior.

  • Methods of the Object class

The Object class provides the following methods:

  • Equals(object obj): Determines whether the current object is equal to another object. By default, it compares object references for reference types and values for value types. You can override this method to provide custom equality logic.
  • GetHashCode(): Returns a hash code for the current object. You should override this method if you override Equals(object obj) to ensure that objects that are considered equal have the same hash code.
  • GetType(): Returns the type of the current object. This method is useful for reflection and cannot be overridden.
  • ToString(): Returns a string representation of the current object. By default, it returns the fully qualified type name. You can override this method to provide a custom string representation.
  • ReferenceEquals(object objA, object objB): Determines whether two object references refer to the same instance. This method is static and cannot be overridden.
  • Overriding Object class methods

You can override the Object class methods to provide custom behavior for your classes. Here's an example of how to override the Equals and GetHashCode methods for a custom Person class:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public override bool Equals(object obj)
    {
        if (obj == null || GetType() != obj.GetType())
        {
            return false;
        }

        Person otherPerson = (Person)obj;
        return FirstName == otherPerson.FirstName && LastName == otherPerson.LastName;
    }

    public override int GetHashCode()
    {
        return HashCode.Combine(FirstName, LastName);
    }
}

In this example, we override the Equals method to compare Person objects based on their FirstName and LastName properties. We also override the GetHashCode method to ensure that equal Person objects have the same hash code.

That's it! You now have a basic understanding of the C# Object class, its methods, and how to override them. The Object class provides a set of methods that are available to all types in C#, making it easier to work with objects in a consistent and flexible way.

  1. Using Object class in C# programming

    The Object class is the base class for all types in C#, and it is implicitly used in the inheritance hierarchy.

    object myObject = new SomeClass();
    
  2. Object class and inheritance in C#

    All classes in C# implicitly inherit from the Object class.

    class MyClass { /* ... */ }
    
  3. Object class methods and properties in C#

    The Object class provides methods and properties such as ToString(), GetHashCode(), and Equals().

    object myObject = new SomeClass();
    string stringRepresentation = myObject.ToString();
    
  4. C# Object class vs. custom classes

    Custom classes in C# implicitly inherit from the Object class. You can override methods for custom behavior.

    class MyCustomClass
    {
        public override string ToString()
        {
            return "Custom ToString Implementation";
        }
    }
    
  5. Object class and type casting in C#

    Type casting can be done using the as keyword or explicit casting.

    object myObject = "Hello, C#";
    string myString = myObject as string;
    
  6. Object class and ToString() method in C#

    The ToString() method is used to get a string representation of an object.

    object myObject = new SomeClass();
    string stringRepresentation = myObject.ToString();
    
  7. Object class and GetHashCode() in C#

    The GetHashCode() method returns a hash code for the object.

    object myObject = new SomeClass();
    int hashCode = myObject.GetHashCode();
    
  8. Object class and Equals() method in C#

    The Equals() method checks if two objects are equal.

    object obj1 = new SomeClass();
    object obj2 = new SomeClass();
    bool areEqual = obj1.Equals(obj2);
    
  9. Object class and reference types in C#

    Reference types use reference equality by default in the Equals() method.

    object obj1 = new SomeClass();
    object obj2 = new SomeClass();
    bool areEqual = obj1.Equals(obj2); // Reference equality
    
  10. Object class and boxing/unboxing in C#

    Boxing converts a value type to an object, while unboxing extracts the value from the object.

    int myInt = 42;
    object boxedInt = myInt; // Boxing
    int unboxedInt = (int)boxedInt; // Unboxing