Redundant to inherit from Object in C#?

后端 未结 7 827
后悔当初
后悔当初 2021-01-07 16:32

As stated above, is it redundant to inherit from Object in c#? Do both sets of code below result in equivalent objects being defined?

class TestClassUno : Ob         


        
相关标签:
7条回答
  • 2021-01-07 17:31

    There is a time when inheriting from object makes sense — when you have a partial class definition, and you want to lock down inheritance and prevent other partial declarations from extending a different class. Attempting to inherit from another type in the partial class will then give you the CS0263 Compiler Error.

    This only makes sense in the event that your partial class is not already inheriting from another type.

    The partial keyword is not in your code example, but I thought it might be worth mentioning as I found my way here from Google after seeing a similar setup in an API. Aside from inheriting from objects that don't point to the official System.Object as others pointed out, there is no benefit in doing so as all classes inherit from object implicitly.

    0 讨论(0)
提交回复
热议问题