Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I assure a class to have a static property by using interface or abstract?

I have one abstract class -let's say myBase. And I want all the classes derived from myBase to have one static field called

public static List<string> MyPArameterNames  { get {return _myParameterNames;}  } 

So, every child class can tell what parameter names it uses; I want static because I do not want to create an instance just for this.

How can I achieve this?

like image 215
pencilCake Avatar asked Mar 14 '10 22:03

pencilCake


People also ask

CAN interface have static property?

A private static field can be set from somewhere inside the interface. But a static method cannot be implemented from outside the interface since it is part of the interface itself.

Can we use interface in static class?

Since you cannot instantiate a static class, static classes cannot implement interfaces.

Can static class inherit abstract class?

NO, we can not inherit static class in c# because they are sealed and abstract. There seems to be no good reason to inherit a static class. It has public static members that you can always access via the class name itself. Any class which does not contain instance contructor, cannot be inherited.

How do you implement property in an interface?

You just specify that there is a property with a getter and a setter, whatever they will do. In the class, you actually implement them. The shortest way to do this is using this { get; set; } syntax. The compiler will create a field and generate the getter and setter implementation for it.


1 Answers

You can't do that. Interfaces, abstract, etc. cannot apply to static members. If you want to accomplish this, you will have to manually remember to do it on all deriving classes.

Also, static members are inherited by deriving classes. Child classes must hide the static parent member if they wish to specify alternate behavior.

like image 146
Zach Johnson Avatar answered Oct 11 '22 01:10

Zach Johnson