DEV Community

Cover image for Why C# Keeps Opening Its Syntax (And Why It Matters)
Akira Shimodate
Akira Shimodate

Posted on

Why C# Keeps Opening Its Syntax (And Why It Matters)

Conclusion

C# has been evolving in a very specific direction:

👉 It is gradually giving language-level privileges to user-defined types.

This is not accidental.
This article explains why.

C# has been evolving in a very specific direction:

Language-level privileges are gradually being opened to user-defined
types.

This article explains that evolution and why it naturally leads to the
idea of giving meaning to values themselves.

Early C#: Permission by Type

In early C#, language constructs were tightly bound to specific types.

foreach (var item in collection)
{
}
Enter fullscreen mode Exit fullscreen mode

This required IEnumerable.

This construct only works if your type implements this interface.

Modern C#: Pattern-Based

Today, it's different.

public class MyCollection
{
    public Enumerator GetEnumerator() => new Enumerator();

    public struct Enumerator
    {
        public int Current => 0;
        public bool MoveNext() => false;
    }
}
Enter fullscreen mode Exit fullscreen mode

The compiler now cares about behavior, not type.

This Pattern Repeats

  • using → Dispose()
  • await → GetAwaiter()
  • deconstruction → Deconstruct()
  • collection initializer → Add()

Language features are no longer exclusive.

Why This Change Happened

Meaning should not be trapped inside APIs.

TimeSpan.FromMilliseconds(123)
Enter fullscreen mode Exit fullscreen mode

vs

123.Milliseconds()
Enter fullscreen mode Exit fullscreen mode

The latter embeds meaning directly into the value.

Syntax as a Shortcut for Meaning

  • APIs are read sequentially\
  • Syntax is recognized instantly

Syntax reduces cognitive load.

The Next Question

Can values themselves carry meaning?

var x = 100;
Enter fullscreen mode Exit fullscreen mode

This has no meaning.

Direction of Evolution

  1. Meaning in types\
  2. Meaning in behavior\
  3. Meaning in syntax

Next:

Meaning in values

Related Proposal

I proposed a concrete idea based on this direction:\
(https://dev.to/shimodateakira/why-cant-user-types-have-literals-in-c-3ln1)

C# is no longer just a language for writing programs.

👉 It is becoming a language for expressing meaning directly.

Summary

C# is evolving from type-centric to meaning-centric.

And naturally:

Toward embedding meaning directly into values.

Top comments (0)