Why are constructors not inherited in C#?

后端 未结 5 2125
耶瑟儿~
耶瑟儿~ 2020-11-29 07:52

I\'m guessing there\'s something really basic about C# inheritance that I don\'t understand. Would someone please enlighten me?

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 08:42

    I assume you mean:

    class Foo
    {
       public Foo(int someVar) {}
    }
    
    class Bar : Foo
    {
        // Why does Bar not automatically have compiler generated version of 
        Bar(int someVar): Foo(someVar) {}
    }
    

    I believe this is inherited from C++ (and Java).
    But assuming you did have this and Bar had some other member variables. Would this not introduce the posability of the compiler generated constructor accdently being used and not initialising the members of BAr.

提交回复
热议问题