Sunday, January 20, 2013

What is the top .NET class that everything is derived from?


Top .NET class

Every class in .NET inherit from System.Object class. To make sure, just create a blank class and create an object of this class. System.Object class contains four default methods and these method can be accessed by any of the class object. It prove that all the classes are inherit from System.Object class.

Methods of System.Object Class:


  • Equals
  • GetHashCode
  • GetType
  • ToSting


Example

    class abc          
    {
        public string MyMethod()
        {
            return "Welcome";
        }
    }  

     class Program
     {
        static void Main(string[] args)
         {
            abc obj = new abc();
            Console.WriteLine("abc class method MyMethod() - " + obj.MyMethod());
            Console.WriteLine("System.Object class method GetHashCode()- " + obj.GetHashCode());
            Console.WriteLine("System.Object class method GetType()- " + obj.GetType());
            Console.WriteLine("System.Object class method ToString()- " + obj.ToString());
            Console.ReadLine();
         }
     }

Output

abc class method MyMethod() – Welcome
System.Object class method GetHashCode()- 45653674
System.Object class method GetType()- ConsoleApplication1.abc
System.Object class method ToString()-ConsoleApplication1.abc

Note.The above code is showing that the abc class contain only one method but we are accessing more methods like GetHashCode, GetType etc. so these methods are coming from base class which is System.Object

No comments:

Post a Comment