The 8 most missing features in C#

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?

I’ve asked the same question here and there (in Polish) and here are aggregated results, sorted by popularity, with some additional remarks from my side:

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:

It has been recently reincarnated (again?). And if you really, really need it, you can use dynamic magic from libraries like impromptu-interface.

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.

Funny

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😍

Summary

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!

4 comments

  1. Good list. I’d like to add a suggestion: compiler-assisted error handling.

    Right now, the only way to see what exceptions a method will throw is to read the documentation of every method, or run the program and see what goes wrong. Both aren’t great.

    Java has checked exceptions, but they’re a pain to use. Having some sort of metadata on a method for the errors, and a much less verbose try/catch syntax would help a lot. e.g. http://joeduffyblog.com/2016/02/07/the-error-model/

  2. I’d love type classes, first class Unit, and maybe discriminated unions.

    However, currently the biggest issue for me is with .NET itself: its lack of support for proper 64-bit arrays and now also Spans. Who’s brilliant idea that was in 2017, that users will never need 64-bit span is beyond me.

Leave a Reply

Your email address will not be published. Required fields are marked *