Feature Request: Inferred static keyword for members in static classes. #1753
Replies: 12 comments
-
I'm not really happy that every member would become an instance member during a refactoring; for example, making the class instantiable or moving members to a different class. |
Beta Was this translation helpful? Give feedback.
-
What @jnm2 said: Today, a static class is always safe to refactor into a non-static class in a later release without breaking any compatibility. This proposal would make such a refactoring a very large breaking change for no reason. |
Beta Was this translation helpful? Give feedback.
-
Having explicitly static members just makes it even more confusing, it would make me forget I'm working in a static class. Also, |
Beta Was this translation helpful? Give feedback.
-
I am with @Spaier in thinking that the static class Extensions
{
public int StaticProperty { get; set; }
public static int ExplicitStaticProperty { get; set; }
public void StaticMethod() { }
public static void ExplicitStaticMethod() { }
} That mixture of members, some static and some not would have to be legal syntax with this proposal, to avoid breaking existing code. But it’s confusing. I reread the OP a couple of times trying to work out the significance of some being marked module Extentions
{
public int Foo() => ...
} Here, the |
Beta Was this translation helpful? Give feedback.
-
@DavidArno You definitely nailed the "getting best of both worlds." I didn't like either that adding Just out of curiosity, if something is a |
Beta Was this translation helpful? Give feedback.
-
It might need to be legal syntactically, but it needn't be legal code. It could simply be a rule that either you specify static for all members, or you don't specify for any. And certainly I'd like to UI to strongly encourage removing all 'statics' by a simple click. But in the scope of things it's a minor annoyance, and I agree if there's not enough in favour of the change it's not worth the effort of getting changed. |
Beta Was this translation helpful? Give feedback.
-
C# has preferred, to this point at least, being quite explicit about many things. Methods and properties aren't virtual by default, you have to opt-in to allowing them to be overridden. You also can't override a method in a descendant class just by redeclaring it - you have to explicitly state override if you want that behavior. Perhaps the biggest reason why I think it's a good that static methods are always explicitly marked as static is that some codebases contain very large classes. (I'm not going to touch on whether that's good or not - but it is definitely true.) Consider this declaration: public string Description() => "Transmogrify the rotation frequency of the shields"; I suggest it would be quite confusing if this method was sometimes static, and sometimes not; just as it would be quite confusing if this method was sometimes virtual, and sometimes not. |
Beta Was this translation helpful? Give feedback.
-
Assuming the difference matters, sure. But if it doesn't - i.e. whether or not you declare a member on a static class as "static" actually makes no difference to the behavior, then it's no more confusing than the fact that you can write either |
Beta Was this translation helpful? Give feedback.
-
It's definitely more confusing. With |
Beta Was this translation helpful? Give feedback.
-
The difference definitely matters to me. When i see a method, knowing whether it is static or not is absolutely a core part of what i want to know about it and how i will think about it. So eliding that information would not be desirable for me at all, and it would actively make things like code review and maintenance harder. |
Beta Was this translation helpful? Give feedback.
-
Well I can't say I understand that in this case, but obviously others feel the same way, so I'll save my energy for changes that would truly enhance the power of C# (like non-nullable references, or discriminated unions/sum types)! (PS. how is 'static' being optional any different to 'private' being optional? You can declare a class with a mixture of members some of which are declared 'private' and others not, and developers as a rule don't seem to get thrown by it). |
Beta Was this translation helpful? Give feedback.
-
This is both a good and a bad thing. For example, it's good that eg methods aren't virtual by default and that they must be explicitly marked as such. The problem arises though when development methods change and the default becomes "wrong". For example, fields are not read-only by default; I have to explicitly mark them as such. And for folk who use a more functionally orientated approach to things, the fact that methods aren't static and pure by default and classes aren't sealed by default becomes a nuisance as nearly everything has to be explicitly marked that way. But there's no easy, tidy solution to this:
Currently, we are stuck on option one as all other options have problems too. Hopefully at some stage, some bright spark will think up another option though that allows progress in this area. |
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.
-
Since everything in
static
classes is static as well it would be nice not to writestatic
keyword in every method, property, etc.Before:
After:
Beta Was this translation helpful? Give feedback.
All reactions