static interface and static abstract class #7260
Replies: 4 comments 6 replies
-
Beta Was this translation helpful? Give feedback.
-
|
I'm struggling to understand how static interfaces and static abstract classes would work. What would you do with them? For example, take the following code: interface IObjectExample
{
void Foo();
}
abstract class ObjectExampleBase : IObjectExample
{
public abstract void Foo();
}
class ObjectExample : ObjectExampleBase
{
public override void Foo() {}
}
void Example(IObjectExample obj) => obj.Foo();
var o = new ObjectExample();
Example(o);I can have a method that takes an But what then happens if this is all static: static interface IClassExample
{
static void Foo();
}
static abstract class ClassExampleBase : IClassExample
{
public static abstract void Foo();
}
static class ClassExample : ClassExampleBase
{
public static override void Foo() {}
}
void Example(IClassExample thing) => thing.Foo();What is It would be interesting to see a use-case for all of this that is compelling compared with using delegates, which seems to me to already cover all this. |
Beta Was this translation helpful? Give feedback.
-
|
Hi there, It is hard to show a good use case because it isn't possible right now. So testing it out and exploring what it can do is impossible right now. Because any property in a static class is also static, you will learn very fast not to do this. I'm also very sorry I even talking about static interfaces and static abstract classes. Thanks for al of you who are willing to even think about these possibilities. WIth kind regards, Ingmar |
Beta Was this translation helpful? Give feedback.
-
|
For me, static abstract classes inheritance would be mostly helpful for programmer himself to ensure that the structure of the class is being followed. Example: SomeClass1 and SomeClass2 provide an API to communicate with an external process. This way, you make sure you haven't forgotten to declare a property required by code structure and you get a helpful method without having to copy-paste it into each class. Moreover, you can quickly get types inheriting from base type via reflection in order to generate some diagnostics output. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This issue has been moved from a ticket on Developer Community.
Hi,
I know static classes are seen as dirty. I know the interface and abstract class need to be instantiated.
Still I would love for there to be a "static interface" - an interface that you could put on a static class.
And I would also love a static abstract base class with static virtual methods that you can override in another static class.
This is because I find it very usefull to do all my inheritance on models, and then have static classes for specific logic.
These business classes are static because you can't instantiate them, and every method has to work only with it's parameters.
Right now, loosing inheritance is a big price to pay, but when all inheritance is already done, I experiance it as a very clean way of programming.
Could you please consider adding an static interface and a static abstract class to our language c#?
With kind regards,
Ingmar
Beta Was this translation helpful? Give feedback.
All reactions