Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • summary

    typeof(ISomeInterface).BaseType is null; typeof(object).BaseType is also null by definition. Thus:

    typeof(object).BaseType==typeof(ISomeInterface).BaseType -> true 
    

    It could (semantically) mean System.Object is an interface, too, doesn't it? In c#, all classes inherit from System.Object, but interfaces do not. Interfaces in fact are contracts, not the base type of any class, and can only derive from other interfaces.

    So I'm wondering whether typeof(object).BaseType should be the same as interfaces?

  • explanation

    I thought this in the way of elementary mathematical logic. I treated null as 0 and typeof(object) as 1, so that finding the base types of some type are just like finding the factors of a number.

    Under this assumption, null could be the derivation of any type, like 0 is a multiple by a factor of any number. And typeof(object) would be the base type of any class, like 1 is a factor of any number even itself. The actual returning null of typeof(object).BaseType, however, broke this assumption. This seems saying that 0 is a factor of 1 but not of any other number.

    Further, if we use a method to find the base type of SomeType recursively, we cannot always say SomeType is not a class because of its BaseType is null, typeof(object).BaseType is also null.

    It seems paradoxical if

    typeof(object).BaseType==typeof(object) -> true 
    

    to present that its base type is itself, but isn't that exactly the difference between entities and contracts?


update:

I originally stated that:

It could mean System.Object is an interface, too, doesn't it?

What I want to say is it seems confused. It's my fault of the poorly expressed description, and sorry to cause the answers focused on that.

like image 814
Ken Kin Avatar asked Dec 23 '12 13:12

Ken Kin


1 Answers

Classes and Interfaces are two different things. The following expression:

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

Is true only because the following is also true:

null == null

And, by definition, BaseType returns null if the current type represents the object class or an interface.

It could mean System.Object is an interface, too, doesn't it?

Actually not, because an interface contains only the signatures of methods, but not their implementation. You can think an interface as a contract that classes must implement.

like image 116
Thomas C. G. de Vilhena Avatar answered Nov 09 '22 17:11

Thomas C. G. de Vilhena