6 ms·
C# started as a language tightly aligned with C++/Java, but has since moved to be something else entirely that is highly capable. I assume that free-floating f
by Genbox 2y ago
C# started as a language tightly aligned with C++/Java, but has since moved to be something else entirely that is highly capable.
I assume that free-floating functions are global functions. You can achieve something similar by "global using". Put this in a file and tug it away somewhere:
"global using static YourStaticClass;"
Now you can call all the static methods on the class everywhere.
As for the using vs. naming convention, most people use the IDE and hover the mouse over the type to see its identity. Once you get proficient with the IDE, you can do it using shortcuts. However, if you really want to prefix all types with a shorthand of the package it came from, you can do it with aliases in C#.
- lordofgibbons 2y ago> I assume that free-floating functions are global functions. In most languages they're bound to some scope, like package, module, etc. I'm not familiar with C#, but I assume there it would be scoped in a namespace.
- ngrilly 2y agoYes, I'd like to be able to define functions directly under the namespace. In C#, it seems the only way to do this in to define a static method in a class, the class being part of a namespace.
- coder543 2y agoI think they wanted to define free functions, not figure out a way to use functions without a class name, but I could be wrong.
- ngrilly 2y agoYes, that's that I meant.
- ngrilly 2y agoI use VSCode with the C# LSP, but I prefer to immediately see where a name comes from by reading it, rather than hovering over it. That's why I prefer to avoid global. Regarding imports, I guess I could do something like `using c = my.namespace.ClassWithMyStaticMethods`, but I suppose it's not idiomatic in C#.
- dmoney_2 2y agoBetter is `using static my.namespace.ClassWithMyStaticMethods` that gets you exactly the consuming syntax you want, even though the methods still need to be static on that class.
- metaltyphoon 2y ago> using c = my.namespace.ClassWithMyStaticMethods Non idiomatic? IMO, this is completely fine. Idiomatic C# doesn't mean OO all the time.
- ngrilly 2y agoGlad to hear. I'll consider this more then.