DEV Community

Cover image for Are User-Defined Literals Necessary in C#?
Akira Shimodate
Akira Shimodate

Posted on

Are User-Defined Literals Necessary in C#?

A Step Toward Meaningful Values

Conclusion

This is not a new idea.

👉 It is a continuation of how C# has been evolving.

Giving meaning directly to values is a natural evolution of C#.

C# has been moving in this direction for years.

This article proposes User-Defined Literals (UDL) as a natural
continuation of that evolution.

The Current Limitation

var duration = 123;
Enter fullscreen mode Exit fullscreen mode

We don't know:

  • seconds?\
  • milliseconds?

No meaning.

Current Solution

123.Milliseconds()
Enter fullscreen mode Exit fullscreen mode

Meaning depends on API.

The Core Problem

Traditionally, meaning is attached after the value is created.

UDL shifts that boundary.

Values themselves have no meaning.

Proposal

var duration = 123_ms;
Enter fullscreen mode Exit fullscreen mode

Now:

  • Type is known\
  • Meaning is known

Meaning is embedded in the value.

Why This Is Natural

C# has already evolved:

  • foreach (pattern-based)\
  • await (pattern-based)\
  • using (pattern-based)

Language privileges have been opened.

See also:\
👉 (https://dev.to/shimodateakira/why-c-keeps-opening-its-syntax-and-why-it-matters-3l7f)

Common Misconception

Literal = compile-time constant

Not true.

var s = $"{DateTime.Now}";
Enter fullscreen mode Exit fullscreen mode

Literal is syntax, not evaluation timing.

Summary

  • APIs attach meaning later\
  • Literals carry meaning immediately

This is not syntax sugar. It's a design shift.

Final Thought

So the real question is:

Should values themselves carry meaning?

And if so:

How far should C# go in that direction?

I'm curious how others see this.

Is this a natural evolution of C#,
or are we just moving complexity from APIs into the language itself?

Top comments (0)