The 8 most missing features in C#
source link: https://tooslowexception.com/the-8-most-missing-features-in-c/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Performance, architecture, Software Craftmanship...
Pro C# 9
Sometimes you may hear oppinions that there are too many changes introduced to the C# and/or too fast. That people get lost and confused about all this new syntax and feature added here and there. And while one may argue or not, I would like to look at this topic from a different angle – what are you missing in C#? What single functionality would you enjoy the most?
1. Discriminated unions
The clear winner is a possibility to use “discriminated unions”. There is an ongoing proposal about them already. The most often mentioned wishes here are to have good pattern matching support (we hope so!) and F# DU compatiliby (unlikely, like in the case with records).
2. First class void/aka Unit
Another functional-like feature is to have void having type representation, like unit in F#. This avoids treating differently methods that return nothing (void X()), and methods that return something (int X()). So, for example, allows more flexible generics handling, as now Action == Func<unit>. If you feel interested, read “Unit isomorphisms” by Mark Seemann explaining that “The C# and Java keyword ‘void’ is isomorphic to a data type called ‘unit””.
3. Pipe operator
Yet another feature making C# more F#. Besides many functional use cases, it would allow, for example, to create readable “fluent” code without writing dedicated extensions methods.
4. Type interference of methods
Having var as method return type seems like kind of natural extension where var can be used currently. In places where the compiler is able to unambiguously infer the return type (greetings Jared;)) we could write:
5. Anonymous interface implementation
A long-living feature request to have possiblity to anonymously (via anonymous objects) implement interfaces:
6. Shapes aka type classes
The idea to allow definining abstractions/extension over types is considered for some time, and with the current proposal it looks like:
But it goes even further in the form of “shapes” (type clases), as the proposal explains: “Interfaces abstract over the shape of objects and values that are instances of types. The idea behind type classes is essentially to abstract over the shapes of the types themselves instead”. So you can define a shape like that:
And then, the primary purpose of a shape is to be used as a generic constraint, so you can write:
Avoiding repeating myself, I encourage you to read the entire proposal and the discussions.
7. Primary constructors
Writing constructors to initialize many fields (dependencies?) is tiresome and requires a lot of names copy-pasting. The idea is to introduce “primary constructor” that automaticaly wires up everything. After introducing source generators, they can be implemented with the help of that. Thus, I’m not sure if they have a future in C#.
8. constexpr from C++
Well, this one is pretty exotic for C# developers probably. C++ allows to use constexpr specifier which “specifies that the value of a variable or function can appear in constant expressions”. But in general, this is more about what and when compiler is able to detect “deterministic output” so compile time constant may be infered/calculated. I invite you to read a pretty broad discussion about it, covering many various use cases and scenarios.
Besides those above-listed, I’ve received more funny (or serious?;)) answers and I would like to share them too:
1. Make .NET faster and faster – ok, this is not funny. It is just more about the runtime, not the C# itself. So while I fully agree, it should not be on the above list.
2. Multiinheritance – yes, we can have sort of it using default implementations in interfaces. But, still it is not the same. Funny joke, right…
3. Coroutines and fixing async/await – I treat it as a joke, while definitely it may be not and it does require runtime changes:)
4. More F#/TypeScript features – sure!
5. string.parseFromUserStory(„…”) – bad naming case, it will not be implemented
6. Inline assembly – always welcomed for performance
Do you like those features and would like to see them added in the next C# versions? And what are you missing in C#? What single functionality would you enjoy the most? Please, share in the comments or social media!
Aggregate valuable and interesting links.
Joyk means Joy of geeK