[You must know. NET] The 27th time: interface in the end inherit object?

Source: Internet
Author: User
Tags instance method

Say, before the opening,

In the. NET world, we often hear the word "System.Object is the root of all types, the parent of all types", so much so that I am in "you must know. Net" 8.1 with "all things return to the Pope: System.Object" Such title for the System.Object Award to high honors. So, based on this view there is the following sentence "Does the interface also inherit from System.Object?" "In fact this is just one of those little discussions in the tech group today.

1 origin

In the. NET world, we often hear the word "System.Object is the root of all types, the parent of all types", so much so that I am in "you must know. Net" 8.1 with "all things return to the Pope: System.Object" Such title for the System.Object Award to high honors. So, based on this view there is the following sentence "Does the interface also inherit from System.Object?" "In fact this is just one of those little discussions in the tech group today.

"Interface also inherits from Object" is based on the following two points of view:

Opinion one:

An interface is also a class in nature, since the interface type is compiled in the IL and is identified as. class, and since it is a class it is inevitable that it eventually inherits from System.Object.

Point two:

If you have the following interface and the type of implementation interface:

// Release : code01, 2009/03/04
// Author : Anytao, http://www.anytao.com
// List  : IObjectable.cs
public interface IObjectable
{
}

// Release : code02, 2009/03/04
// Author : Anytao, http://www.anytao.com
// List  : MyObject.cs
public class MyObject : IObjectable
{
}

Then, for the Iobjectable object, the following call is possible:

// Release : code03, 2009/03/04
// Author : Anytao, http://www.anytao.com
// List  : Program.cs
class Program
{
   static void Main(string[] args)
   {
     IObjectable obj = new MyObject();

     //Call Object instance methods
     obj.ToString();
     //Call Object static methods
     IObjectable.Equals(null, null);
   }
}

Obviously, iobjectable type variable obj can access instance method ToString () and virtual method Equals that exist in System.Object, and of course several other public services are no exception: GetType (), Equals (), GetHashCode (), ReferenceEquals (), can also infer clues to the interface access to the object method.

Admittedly, part of the reasoning above is completely correct, but unfortunately led to the wrong answer, so in this article I will definitely find out: interface does not inherit from the reason and principle of object. For a deep discussion of the nature of the interface, refer to the detailed analysis of the. NET 1.5 "You Must Know", "Topsy Interface" and 7.4 "object-oriented programming: Interfaces and abstract classes."

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.