C#

C# Root Class – Object

Programmingempire

In this post, I will explain C# Root Class – Object which every class inherits from. Specifically, each type in C# has a base class, whether it is a reference type or a value type. Particularly, this base class is called Object. Also, the Object class provides several methods that any class can either use or override.

Significance of the Root Object Class

In order to understand the significance of the root object class, let us consider an example in which we create a common method that works on any type. Before that let us understand how to retrieve the type and base type information of an object.

Type and Base Type of an Object

We use the GetType() method of the class Object to retrieve the type of an object. Besides the BaseType property returns the type of the base class as shown in the following example.

using System;

namespace TypesDemo
{
    class A { }
    class B : A { }
    class Program
    {
        static void Main(string[] args)
        {
            A ob1 = new A();
            B ob2 = new B();

            Console.WriteLine($"The type of ob1 is {ob1.GetType().ToString()}");
            Console.WriteLine($"The type of ob2 is {ob2.GetType().ToString()}");

            Console.WriteLine($"The Base Type of ob1 is {ob1.GetType().BaseType.ToString()}");
            Console.WriteLine($"The Base Type of ob2 is {ob2.GetType().BaseType.ToString()}");
        }
    }
}

Output

Retrieving the Type and Base Type Information
Retrieving the Type and Base Type Information

Evidently, class in the above program has a base class System.Object whereas the class has its immediate base class as A.

Example of Using Object class in Passing Parameter in a Method

The following example shows that we have a method CompareObjects() residing in the class MyUniversalClass. Since this method takes as parameters, the two objects of type Object, this method can work on all types. Further, in the Main() method we call this method using parameters of both value types and reference types. Therefore, we can have a single method that works on different types of objects.

using System;

namespace ObjectClassDemo
{
    class MyUniversalClass
    {
        public void CompareObjects(Object x, Object y)
        {
            String t1 = x.GetType().ToString();
            String t2 = y.GetType().ToString();

            String t3 = x.GetType().BaseType.ToString();
            String t4 = y.GetType().BaseType.ToString();

            Console.WriteLine($"Type of x is {t1}, Type of y is {t2}");
            Console.WriteLine($"Base Type of x is {t3}, Base Type of y is {t4}");

            if (t1.Equals("ObjectClassDemo.Box") && t2.Equals("ObjectClassDemo.Box"))
            {
                Box b1 = (Box)x;
                Box b2 = (Box)y;
                if (b1.Volume() < b2.Volume())
                {
                    Console.WriteLine("First Box is smaller!");
                }
                else if (b1.Volume() == b2.Volume())
                {
                    Console.WriteLine("Both boxes have same volume!");
                }
                else
                {
                    Console.WriteLine("Second Box is smaller!");

                }
            }
                if (t1.Equals("ObjectClassDemo.Student") && t2.Equals("ObjectClassDemo.Student"))
                {
                    Student s1 = (Student)x;
                    Student s2 = (Student)y;
                    if(s1.Age<s2.Age)
                    {
                        Console.WriteLine("First Student is younger!");
                    }
                    else if(s1.Age==s2.Age)
                    {
                        Console.WriteLine("Both students have same age!");
                    }
                    else
                    {
                        Console.WriteLine("Second Student is younger!");
                    }
                }

            }

        }
    
    class Student
    {
        String name, course;
        int age;
        public int Age
        { get => age; }
        public Student() { }
        public Student(String name, String course, int age)
        {
            this.name = name;
            this.course = course;
            this.age = age;
        }
        public override string ToString()
        {
            return $"Student Name: {name}, Course: {course}, Age: {age}";
        }
    }
    class Box
    {
        int d1, d2, d3;
        public Box()
        {
            d1=d2=d3 = 1;
        }
        public Box(int d1, int d2, int d3)
        {
            this.d1 = d1;
            this.d2 = d2;
            this.d3 = d3;
        }
        public int Volume()
        {
            return d1 * d2 * d3;
        }
        public override string ToString()
        {
            return $"Box Width: {d1}, Height: {d2}, Depth: {d3}, Volume: {d1*d2*d3}";
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int a=34, b=78;
            String s1="abcd", s2="aBBc";
            double d1=28.0, d2=27.999989;

            Box b1 = new Box(7, 3, 12);
            Box b2 = new Box(5, 13, 8);

            Student ob1 = new Student("A", "MCA", 20);
            Student ob2 = new Student("B", "BCA", 18);

            MyUniversalClass muc = new MyUniversalClass();
            muc.CompareObjects(a,b);
            muc.CompareObjects(s1, s2);
            muc.CompareObjects(d1, d2);
            muc.CompareObjects(b1, b2);
            muc.CompareObjects(ob1, ob2);
        }
    }
}

Output

Demonstration of C# Root Class - Object
Demonstration of C# Root Class – Object

Benefits and Drawbacks of the Object Class

Apart from providing several useful functions like Equals(), ToString(), and GetType(), the Object class provides us the facility for handling different objects in a common way. Also, it provides us the provision of boxing and unboxing. However, this feature has a drawback in terms of performance, since boxing and unboxing increase the execution time.

Summary

In this article on C# Root Class – Object, the significance of the Object class is explained. In fact, every type irrespective of whether it is a reference type or a value type is derived from the Object class. Further, this class offers several useful methods that the instance of any type can use. This article also describes how to retrieve information about the type as well as the base type of an object.


programmingempire

You may also like...