简体   繁体   中英

Query regarding static class in c#

Hi all I just wanted to know whether a static class which inherits another class, can have access to the parent classe's non static members or not? Please help. Thanks in advance.

A static class cannot inherit or implement any class or interface.

The point of inheriting or implementing a class or interface is to allow instances of your class to be used as the base type.
Since static classes cannot have instances, there's no point.

How should that work? A static class cannot be instantiated and therefor it will never have access to any non static members.

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type

You can view more information about static classes.

static class cannot inherited.

I think you can go with the same concept with SingleTon class and you can inherit the same.

A static class can't inherit from any classes or implement any interfaces.

A static class does implicitly inherit from Object , though. But since it's (also implicitly) abstract you can never have any instances of it, and so can never call any instance methods in Object . Also, it's (implicitly) sealed, and as such cannot have subclasses that would be instantiatable. As a corollary to these characteristics, it can't be used to type any variables, fields or parameters; and it can't be used as a type parameter (if these were possible, null would be the only valid value for such references).

Given all this, a static class does not look like a class at all , and I think would be better represented as a module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM