top | item 42897403

(no title)

Genbox | 1 year 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#.

discuss

order

ngrilly|1 year ago

I 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#.

metaltyphoon|1 year ago

> using c = my.namespace.ClassWithMyStaticMethods

Non idiomatic? IMO, this is completely fine. Idiomatic C# doesn't mean OO all the time.

dmoney_2|1 year ago

Better 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.

lordofgibbons|1 year 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|1 year ago

Yes, 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|1 year ago

I 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|1 year ago

Yes, that's that I meant.