4 ms·
I wish C# had standalone functions without needing to wrap them in classes.
by candl 10y ago
I wish C# had standalone functions without needing to wrap them in classes.
- lacampbell 10y agoI don't think any .NET language can do that - ie in F# every function has to be wrapped up in a module.
- louthy 10y ago> I don't think any .NET language can do that Although the CLR does allow for them. Global functions can be emulated: public static GlobalFuncs { public string Greet(string x) => $"Hello, {x}"; } Elsewhere: using static GlobalFuncs; public void Test(); { Greet("HN"); // "Hello, HN" }
- jarnonijboer 10y agoThis isn't feature that the CLR knows. It is just syntactic sugar from the C# compiler; As it will prepend GlobalFuncs to the Greet method.
- louthy 10y agoSorry, I didn't mean to confuse the issue. The CLR can do global functions. But no .NET language currently supports it. The example I gave was just a way of achieving visually the effect of global functions (which I assumed was the major issue of the OP, because there's no other cost to using static methods than having the qualify them with the type name).
- Nition 10y agoIt'd never pass code review on Hacker News but the classic C# hack for that is just to make a class called Global or whatever and throw your must-be-everywhere functions in there.
- swalsh 10y agoI think most applications in C# end up with a Utilities class/library.
- ruleabidinguser 10y agoc++ namespaces are so great for this. Don't know why theyre not available in most languages.
- Pxtl 10y agoUsing static lets you ape that now. You still need to declare a static class, but you don't need to include the class name when calling it if you important the class via "using static".