r/csharp • u/Zardotab • Feb 24 '21
Discussion Why "static"?
I'm puzzled about the philosophical value of the "static" keyword? Being static seems limiting and forcing an unnecessary dichotomy. It seems one should be able to call any method of any class without having to first instantiate an object, for example, as long as it doesn't reference any class-level variables.
Are there better or alternative ways to achieve whatever it is that static was intended to achieve? I'm not trying to trash C# here, but rather trying to understand why it is the way it is by poking and prodding the tradeoffs.
0
Upvotes
View all comments
Show parent comment
1
u/Zardotab Feb 26 '21 edited Feb 26 '21
As I mention in another reply, an anonymous object is instantiated. Typically any changes to it would be inconsequential for the "inline" call style. It's "temporary" local state. If the compiler is smart enough, it may know to skip steps that generate things that are never read from, but this "trimming" is not a necessity to implementing the idea, only a bonus.
What are those ways? They look cluttered to me. It's why I introduced the FormValidation scenario to explore something specific.
Perhaps because I use dynamic languages fairly often, blurring the distinction between class and object is more "natural" to my mind. Instantiation is pretty much just "cloning" the class (prototype object). Those not used to this view may find it strange, as the class/object dichotomy is a strong concept in their mind. That could explain some of the negative Reddit points I got.
Compiled/static languages can more or less do the same thing, but with more intent & restriction tags to control things better.