L
L
ldmitriy2020-06-04 08:53:16
C++ / C#
ldmitriy, 2020-06-04 08:53:16

How does inheritance work in C#?

class A
    {
        public int X { get; set; }
        public int Y { get; set; }

        public A(int x, int y)
        {
            X = x+1;
            Y = y+1;
        }
    }

class B:A
    {
        public int X { get; set; }
        public int Z { get; set; }

        public B(int x, int y, int z):base(x,y)
        {
            X = x;
            Z = z;
            Console.WriteLine($"this.X = {X}, this.Y = {Y}, this.Z = {Z}");
            Console.WriteLine($"base.X = {base.X}, base.Y = {base.Y}");
        }

    }

B b = new B(1,2,3);


1) base is a reference to an instance of the base class?
2) If I generally declared a constructor in the class, but did not initialize the fields, then why do they have default values?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Space Purr, 2020-06-04
@ldmitriy

1. base is a keyword
2. Variables declared in the class body (field) are automatically assigned a default value according to the declared type. This, of course, also applies to properties.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question