Do Interfaces Derive from System.Object? C# Spec Says Yes, Eric Says No, Reality Says No

Do interfaces derive from System.Object? C# spec says yes, Eric says no, reality says no

It's not quite as simple a question as you might think :)

Interfaces don't derive from object but you can call the members of object on them. So you can call ToString() on an expression which has a compile-time type of IDisposable, for example.

Coincidentally, I overhead a conversation between Neal Gafter and Eric at NDC discussing exactly this point...

I believe section 4.2.2 of the spec is over simplified, unfortunately. Hopefully Mads and Eric will fix it up for a future release - I'll mail them to make sure they see this question.

I'm also struggling to find anything in the spec to back up the rest of this answer. Section 3.4.5 of the C# 4 spec comes as close as I can find:

The members of an interface are the members declared in the interface and in all base interfaces of the interface. The members in class object are not, strictly speaking, members of any interface (13.2). However, the members in class object are available via member lookup in any interface type (7.4).

The conversion from an interface type to object is covered by section 6.1.6:

The implicit reference conversions are:

  • From any reference-type to object and dynamic.

In C#,interface Type didn't inherit from System.Object?

Interfaces themselves aren't objects in a class hierarchy. They are "types" in the sense that they represent an object. But they aren't concrete classes in and of themselves.

For example, you can't instantiate an IList<int>. This wouldn't compile:

var list = new IList<int>();

However, you can create a List<int>, which does inherit from System.Object:

var list = new List<int>();

This instance of list can be viewed as lots of different interfaces. For example:

var listVariantA = list as IList<int>();
var listVariantB = list as IEnumerable<int>();

And so on. This is because its concrete type, List<int>, implements multiple interfaces. It doesn't inherit from them, as they themselves are not concrete types.

It seems like where you're getting lost is in the different between inheritance and interfaces. Some might say that interfaces are ".NET's answer to multiple inheritance" which can be a fair statement. But there's a fundamental difference between the two concepts. Think of inheritance as a hierarchy of what an object is:

Vehicle
Car
Honda Accord
Toyota Camry
Truck
Ford F150

And so on. Interfaces don't fit into this hierarchy. Think of an interface, instead, as a contract which an object meets. For example, all Vehicle object might implement the IDrivable interface. Some of them might implement the IFlyable interface. Some still might implement both (flying cars).

Should BaseType of System.Object be the same as interfaces?

The BaseType property returns the base type for types that have a base type. For all other types, it returns a value indicating that the type does not have a base type. You posed the following expression in the original question:

typeof(object).BaseType == typeof(SomeInterface).BaseType

Then you suggested that since this expression is true, Object and SomeInterface appear to have the same base type. However, your conclusion is flawed because it assumes the existence of a base type for both Object and SomeInterface, when the documentation for the method explicitly states that this is not the case.

How come an interface instance has GetType, GetHashCode, Equals and ToString methods?

Your class implicitly derived from System.Object :

Languages typically do not require a class to declare inheritance from Object because the inheritance is implicit. [MSDN]

UPDATE :

Just found this, possible duplicate? :

Do interfaces derive from System.Object? C# spec says yes, Eric says no, reality says no

Is System.object is primitive itself?

1) system.object is itself primitive type?

No. From Type.IsPrimitive:

The primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, and Single.

How System.object catch the arguments(string,int..) in the methods like

There's a conversion from every non-pointer type to object.

how the object of the type base class is instantiated with Derive Class

It isn't. You need to distinguish between three things:

  • A variable (objct in your example)
  • A reference (just a way of getting to an object)
  • The object itself

The value of objct is just a reference. The idea is that you can convert an expression of type "derived type" to its base type (or interface it implements) without actually changing the reference itself.

This is basically the root of polymorphism in .NET - and too big a topic to do true justice to here; I would strongly suggest that you read a good introductory book on C# to get a good grounding on this.

Is there any reason to declare optional parameters in an interface?

Example:

public interface IService1
{
void MyMethod(string text, bool flag = true);
}

public class MyService1a : IService1
{
public void MyMethod(string text, bool flag) { }
}

Usage:

IService1 ser = new MyService1a();
ser.MyMethod("A");

2nd parameter passed to MyService1a will be true, as default parameter in interface.



Related Topics



Leave a reply



Submit