6

The 8 most missing features in C#

 3 years ago
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.
The 8 most missing features in C# – TooSlowException

Performance, architecture, Software Craftmanship...

Diagnostic expert!

Pro C# 9

See my newest Pro .NET Memory Management book. More details:

OutOfMemory!

See my OutOfMemory! card game for developers. More details:

KONRAD KOKOSA

10+ years of developer's live. Big .NET enthusiast, but looking with curiosity at other technologies. Currently working as independent consultant. In love with performance and architecture topics, such like scalability, performance troubleshooting, monitoring and testing. Passionate in blogging, tweeting and public speaking. Founder of Warsaw Web Performance group, first web performance-dedicated user group in Poland. One of DotNetos. Microsoft MVP.
Search

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:

public var M() {
   return 0;

5. Anonymous interface implementation

A long-living feature request to have possiblity to anonymously (via anonymous objects) implement interfaces:

public class C {
  public void M2(IMyInterface iface)
  public void M1()
    M2(new {
         Value = 44
public interface IMyInterface
  int Value { get; }

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:

public extension IntZero of int
  public static int Zero => 0;
WriteLine(5 + int.Zero); // in the scope of the extension, int has a Zero property

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:

public shape SGroup<T>
  static T operator +(T t1, T t2);
  static T Zero { get; }

And then, the primary purpose of a shape is to be used as a generic constraint, so you can write:

public static AddAll<T>(T[] ts) where T : SGroup<T> // shape used as constraint
  var result = T.Zero; // Making use of the shape's Zero property
  foreach (var t in ts) { result += t; } // Making use of the shape's + operator
  return result;

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!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK