Skip to content
  • English Deutsch

benjamin-abt.com

Performance is a feature | Cloud - AI - Azure - .NET - Sustainability

  • Home
  • Blog
  • Portfolio
    • Overview
    • Workshops & Training
    • Publications
  • Contact
  • Home
  • Blog

Why you should no longer use var in C#

Why you should no longer use var in C#
September 9, 2022 4 min read BEN ABT
Csharp .NET

This article was written in 2022. Versions, tools and recommendations may have changed since then. Newer posts are collected under Csharp.

Beginning with C# 3, variables that are declared at method scope can have an implicit “type” var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type.

With this section var is described in the documentation of C# .

So, as you can read, the var keyword came from C# 3 and is thus already a few years old, to be more precise, the release of C# 3 was almost 15 years ago. At that time, the language was still relatively young and the ecosystem as a whole - Visual Studio, Visual Studio Code, Azure DevOps, GitHub or overall Code Reviews and Open Source - was also different.

One of the features why var was introduced was implicit typing: you didn’t have to write the type twice and the compiler simply recognized what type var must be based on the assignment.

C#
1var list = new List<string>();
2// The compiler knows, var must be of List<string>

Why var is bad?

So var had its place in its time, but what are the disadvantages of var?

Type

Many mainly advance the argument that the type no longer needs to be written twice.

C#
List<string> list = new List<string>();

That is correct. However thank God meanwhile a simplified syntax sugar exists, which makes this argument obsolete.

C#
List<string> list = new();

Regarding the type, however, there are other scenarios that play a role in everyday life here and that is the use of interfaces during initialization.

C#
IList<string> list = new List<string>();

This is a notation you see very often - even in open source projects. On its own, this is more a matter of style, but there is often a programmatic use case behind such a style.

C#
1IList<string> list;
2if(condition)
3{
4   list = MyCollectionFactory.CreateCollection();
5}
6else
7{
8   list = new List<string>();
9}

So this is a scenario in which var simply does not work - and for which var was not designed, but misused. It automatically becomes inconsistent - unavoidable.

Even worse are the workarounds you see when var is abused.

C#
1var list = new List<string>() as IList<string>;
2if (condition)
3{
4   list = MyCollectionFactory.CreateCollection();
5}

Result: an unnecessary allocation of the list as soon as the condition occurs. This is bad code.

Naming

Many supporters of var say that you don’t need the type because the name of methods and variables says everything necessary.

Nice wishful thinking that makes me wonder if people really write code. Yes, in theory this should be so - everyday life is far from it: often it can be recognized neither by the method name nor by the variable name, which concrete type comes back.

Of course, now you will hear the statement that you should just learn the naming. Haha! With naming, one thing is guaranteed: it will never work and will never be consistent.

If you see this code, without context: what type does var have?

C#
var proxy = ProxyFactory.For<IUserClient>( );

Is it rather

C#
Proxy<IUserClient> proxy = ProxyFactory.For<IUserClient>( );

or is it

C#
IUserClient proxy = ProxyFactory.For<IUserClient>( );

Naming is never perfect. Context is necessary, if you use var.

Reviewing and IntelliSense

And that is the main reason against var. Most of us now live in a world where source code is not always read only in an IDE. There are more and more scenarios where we don’t have compiler and IntelliSense support.

Without IntelliSense or type help I have no chance to recognize which concrete type it is or which one comes back from a method. We now live in a world where source code is part of documentations or published as open source, for example on GitHub and in markdown files.

It is an insane hurdle when I have to click through umpteen calls in documentation or source code in the browser to find out which type comes back. What’s the point? You make it unnecessarily difficult for the users of the documentation or source code!

Please: Stop it.

It’s 2022, soon 2023. Know your language.
There are better options than var today that do not place unnecessary barriers.

Related articles

Production Rate Limiting in ASP.NET Core

Nov 3, 2026 - 16 min read

Production Rate Limiting in ASP.NET Core

Most ASP.NET Core rate-limiting examples stop after registering a fixed window. That is enough to demonstrate the middleware, but it is not …

The Big Decimal Bugfix with .NET 11

Nov 2, 2026 - 11 min read

The Big Decimal Bugfix with .NET 11

For more than two decades, (decimal)1.23 returned 1.23m. It looked correct, it passed every test and it was a polite lie.

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Oct 27, 2026 - 10 min read

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Distroless, Chiseled and Alpine are often compared as three equivalent Linux distributions. They are not. Alpine is a distribution built …


BEN ABT

BEN ABT

Ben is Chief Engineering Officer and Principal Software Engineer at Medialesson.de, with a strong focus on .NET and Microsoft Azure. In his professional role, he designs and builds highly scalable platforms for hybrid and cloud-based scenarios. He also advises companies and C-levels on migrating to the sovereign cloud.

He runs myCSharp.de, the largest and most active German-speaking C# community forum, actively contributes to both his own and external open-source projects and regularly shares his knowledge through his blog, at community events, conferences and as a writer for technical magazines.

Let's Work Together

Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.

New Platforms

Modernization

Training & Consulting

Contact Me
X Facebook LinkedIn WhatsApp
Back Next Blog Post

Open for work!

Contact Me
BEN ABT BEN ABT Software Engineer · Microsoft MVP Passionate about .NET, Azure, performance optimization and sustainable software engineering.

Medialesson GmbH Logo

Microsoft MVP Logo

Recent posts

View all
Production Rate Limiting in ASP.NET Core

Production Rate Limiting in ASP.NET Core

Nov 3, 2026
The Big Decimal Bugfix with .NET 11

The Big Decimal Bugfix with .NET 11

Nov 2, 2026
Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Oct 27, 2026
Stop Using nginx as Your Default Static File Server in Docker

Stop Using nginx as Your Default Static File Server in …

Oct 24, 2026
Containerizing .NET in 2026 Without a Dockerfile

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026

Tags

.NET (195) Csharp (99) ASP.NET Core (49) Azure (41) DevOps (17) Docker (15) Microsoft (14) EF Core (11) Microsoft MVP (11) Security (11) AI (10) GitHub (10) GitHub Copilot (10) ASP.NET (9) Synology (9) .NET 11 (8) Architecture (8) Raspberry (8) Azure Functions (7) Cloudflare (7)
benjamin-abt.com

About BEN

Benjamin Abt is a Software Engineer, Platform Architect and Microsoft MVP specializing in .NET, Azure, cloud architecture, performance optimization and sustainable software development.

Connect

  • Follow me on GitHub
  • Follow me on LinkedIn
  • Follow me on Mastodon
  • Microsoft MVP profile
  • Follow SchwabenCode on GitHub
  • Follow me on X

Quick Links

  • Portfolio
  • Blog
  • Workshops & Training
  • Publications
  • Get in Touch
  • Sitemap
Open for Work

© Copyright Benjamin Abt & SchwabenCode | 2011 - 2026

Content licensed under CC BY 4.0 unless otherwise noted.

  • Impressum
  • Privacy
  • Contact