.NET class inherits object class by default. How?

后端 未结 3 1711
猫巷女王i
猫巷女王i 2021-01-22 04:54

When we create a class Product

class Product
{
}

What I have learned from different blogs and books that Object class is the super

3条回答
  •  一整个雨季
    2021-01-22 05:20

    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.

提交回复
热议问题