2

When we create a class Product

class Product { } 

What I have learned from different blogs and books that Object class is the super/base/parent class of every class including user-defined classes.

How does this happens?

I assume it is taken care by .NET Framework? If its the case then I am curious to know how does this happens.

2
  • Why is that important? It's just a language implementation detail. So the type system just needs to include always System.Object. Commented Feb 13, 2015 at 13:09
  • It is taken care by the compiler, cause you do not want to write each time the inheritance to object, like class Product : Object {}. Commented Feb 13, 2015 at 13:16

3 Answers 3

5

As per MSDN:

Supports all classes in the .NET Framework class hierarchy and provides low-level services to derived classes. This is the ultimate base class of all classes in the .NET Framework; it is the root of the type hierarchy.

And why do you not 'see' this in the code:

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

So in short the .Net type system does this implicit.

Sign up to request clarification or add additional context in comments.

8 Comments

I guess that this quote doesn't answer the question since it basically repeats what OP has said. I think he wants the technical background behind this "implicit" inheritance.
Well as I see it, it does, because 'how' this happens is implicit.
I was actually looking for And why do you not 'see' this in the code:. I was not able to put it in right words.
@Ric.Net So you mean CTS does it behind the scenes?
I would surely do it in 2 mi. more :)
|
3

Yup, it's taken care by .Net Framework. Every Type inf is stored in a special table in memory when program is running. And type record contains link to a base class record. Every type that is not derived from any class contains a link to an object record.

Through this every type has methods, that are defined in a MethodTable (also in memory when .Net framework is running) as methods of object class. For example GetHashCode or Equals

Comments

2

When you want to explore how things happen, you can always take it one step lower and look into the emitted metadata and IL code generated for each method.

For example, this is a Person class with one property, Name:

public class Person { public string Name { get; set; } } 

When we look at the emitted IL we see (using ILSpy) :

.class public auto ansi beforefieldinit ConsoleApplication2.Person extends [mscorlib]System.Object { // Property declaration removed for brevity } 

The compiler add the extends [mscorlib]System.Object, where mscorlib is the declared assembly, System is the namespace and Object is the declared type.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.