Nugget 23: Modern C# Features Help You Keep Token Money in your Wallet!

Recently I was looking at a C# feature, introduced for classes with C# 12 in 2023. The feature in question is the introduction of a Primary Constructor. 

The feature itself is a sensible evolution. It helps reduce, what is considered today "boiler plate code," even if we are talking about the glorified constructor used to inject dependencies and set parameters.

You may push back and say that, it is all sugar coating an old donut. Ok, sure, but I invite you to look at it closer from a different perspective.

This Nugget is not about the merits of Primary Constructor, but about Token Economy Management (TEM) when you pay attention at what the feature does through the lens of AI-Code Generation and AI-Analysis Tools.

While the feature was originally designed for humans to reduce keyboard typing, the timing of its evolution—expanding from records in C# 9 to classes in C# 12 —perfectly aligns with the boom in LLM-driven development.

Microsoft would likely object. "Nonsense, Dr. Watson! Primary constructors were never designed with AI token optimization in mind!"

Perhaps.

Yet the timing is amusing. Language features that reduce boilerplate began appearing just as LLMs started charging us for every token they read and write. I have no evidence the language designers were thinking about Token Economy Management, but if this was merely a happy accident, it was an exceptionally fortunate one. I like it.

Consider the following.

For an AI model, generating text is sequential. The fewer tokens it must generate, the faster the response can be produced. Eliminating 10–15 lines of boilerplate means the AI finishes writing the class infrastructure in fractions of a second and gets straight to the business logic you actually care about.

How many times have you asked GitHub Copilot—"Analyze OrderProcessingService and blah, blah, blah...."

Depending on how you write OrderProcessingService, you could be reaching for your General Meeting Pizza Budget to pay for token processing.

Take a look at these two classes. Both implementations provide the same behavior in this example, however, TEM is right on your nose.

But there is more. AI models operate on probabilities. 

The more tokens they have to generate, the higher the mathematical statistical probability that they will make a trivial mistake. We have all seen that:

"Jimmy! Copilot did it all wrong!"

 Misplacing a semicolon, swapping a variable name in an assignment, or messing up an indentation block could be enough to create the right condition for the tool to hallucinate. 

By delegating the generation of fields and assignments to the compiler rather than the LLM, the surface area for AI mistakes drops significantly.

Experiment

To run the following experiment, install this package:

dotnet add package Microsoft.ML.Tokenizers



Result 

==================================================

      COPILOT TOKEN OPTIMIZATION EXPERIMENT

==================================================

Traditional Class Token Count:       175

Primary Constructor Token Count:   74


Tokens Saved:                             101

Context Window Reduction:        57.71%

==================================================


Conclusion

Making your code less verbose with the help of newer language features, such as the Primary Constructor in C#, can influence TEM favorably, and improve the efficiency of how, yourself, your team, of your entire organization uses AI Code tools.

Imagine a 20 year-old enterprise solution.

  • 500 services
  • 1,500 classes
  • Endless constructor injection
  • Backing fields everywhere
  • Repetitive assignment statements
The LLM cannot filter all that code, boilerplate, and will need to be processed. Everyone of those tokens enters the context window. Everyone must be embedded, attended to and reasoned over.

It is not free.

This Nugget only looked at the Primary Constructor, from a different angle.

Comments

Popular posts from this blog

Nugget 11: Adding Filtering to a Blazor QuickGrid

Nugget 9: Building a JobBank App using Blazor. A Nugget with a Story.

Nugget 14: Connection to MongoDB Atlas - SSL Alert 80