DEV Community

Cover image for C# Features I Wish I Had When I Started in 2016
Umer Farooq
Umer Farooq

Posted on AI-assisted

C# Features I Wish I Had When I Started in 2016

In 2016, my day looked like this. A WebForms page, a Page_Load method, an if (!IsPostBack) check, and Request.QueryString["id"] to find out which record to load. On my own time, I built WinForms apps, because dragging a button onto a form and double-clicking it felt like magic.

C# 6 was the current version. It was good. But looking back after ten years, I wrote a lot of code that only existed because the language couldn't do better yet.

Here are the C# features I wish I had back then, and the code I wrote while waiting for them. At the end, I want to hear yours.

 

TL;DR

Seven features, from C# 9 to C# 15, that remove code I used to write every day: Program class ceremony, long new lines, messy multi-line SQL strings, hand-written backing fields, method calls that should have been properties, if (x != null) wrappers, and goto in nested loops.

 

Feature C# version Released
Top-level statements C# 9 2020
Target-typed new() C# 9 2020
Raw string literals C# 11 2022
The field keyword C# 14 2025
Extension members C# 14 2025
Null-conditional assignment C# 14 2025
Labeled break and continue C# 15 2026

1. Top-Level Statements (C# 9)

Every small console tool I wrote started with the same ceremony. A namespace, a class, a Main method, and finally one line that did the actual work.

Then:

using System;

namespace ReportTool
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Generating daily report");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Now:

Console.WriteLine("Generating daily report");
Enter fullscreen mode Exit fullscreen mode

The compiler generates the Program class and Main method for you. For quick tools and experiments, the file now shows only what matters.


2. Target-Typed new() (C# 9)

This one is my favorite. With var, local variables were already short. But var doesn't work for fields, so field declarations repeated the full type twice.

Then:

private Dictionary<int, List<Visitor>> _visitorsByDay =
    new Dictionary<int, List<Visitor>>();
Enter fullscreen mode Exit fullscreen mode

Now:

private Dictionary<int, List<Visitor>> _visitorsByDay = new();
Enter fullscreen mode Exit fullscreen mode

The type is written once, on the left. The line stays short after the = sign, and that's exactly why I like it.


3. Raw String Literals (C# 11)

For reporting screens, I often used ADO.NET with handwritten SQL, because I only needed to read data based on search filters. For inserts and updates, I used LINQ. So I wrote a lot of SQL inside C# strings.

I used @"" verbatim strings for this. They handled multiple lines, but they had two habits I never liked.

Then:

string sql = @"SELECT v.FullName AS ""Visitor Name"", v.CheckInTime
FROM Visitors v
WHERE v.CheckInTime >= @from
  AND v.CheckInTime < @to
ORDER BY v.CheckInTime";
Enter fullscreen mode Exit fullscreen mode

First, every double quote inside the SQL had to be doubled, like ""Visitor Name"". Second, the SQL had to hug the left margin. If I indented it to match my C# code, all those spaces became part of the string.

Now:

string sql = """
    SELECT v.FullName AS "Visitor Name", v.CheckInTime
    FROM Visitors v
    WHERE v.CheckInTime >= @from
      AND v.CheckInTime < @to
    ORDER BY v.CheckInTime
    """;

cmd.Parameters.Add("@from", SqlDbType.DateTime).Value = from;
cmd.Parameters.Add("@to", SqlDbType.DateTime).Value = to;
Enter fullscreen mode Exit fullscreen mode

The query looks exactly like it does in SQL Server Management Studio. The common indentation is removed automatically, and quotes inside the text don't need escaping.

One important note. Raw strings only make the query readable. Filter values still go in as parameters, never by pasting them into the string. That's what keeps the query safe from SQL injection.


4. The field Keyword (C# 14)

In WinForms and WPF, data binding needs INotifyPropertyChanged. That meant a private backing field for every property that raised a change event. I wrote this pattern so many times that it stopped feeling like a problem. It just felt like the standard way.

Then:

private string _visitorName;

public string VisitorName
{
    get { return _visitorName; }
    set
    {
        if (_visitorName == value) return;
        _visitorName = value;
        OnPropertyChanged(nameof(VisitorName));
    }
}
Enter fullscreen mode Exit fullscreen mode

Now:

public string VisitorName
{
    get;
    set
    {
        if (field == value) return;
        field = value;
        OnPropertyChanged(nameof(VisitorName));
    }
} = "";
Enter fullscreen mode Exit fullscreen mode

field refers to the backing field the compiler creates for you. No more _visitorName declared separately, and no chance of using the field by mistake somewhere else in the class.


5. Extension Members (C# 14)

Enums were my biggest struggle in WinForms. An enum value like PendingApproval had to show as "Pending Approval" in a grid or a label. I used the Humanizer library for that sometimes, or a [Description] attribute with an extension method.

Then:

public enum VisitStatus
{
    [Description("Pending Approval")]
    PendingApproval,

    [Description("Checked In")]
    CheckedIn,

    [Description("Checked Out")]
    CheckedOut
}

public static class VisitStatusExtensions
{
    public static string GetDisplayName(this VisitStatus status)
    {
        var member = typeof(VisitStatus).GetMember(status.ToString());
        var attribute = member[0].GetCustomAttribute<DescriptionAttribute>();
        return attribute != null ? attribute.Description : status.ToString();
    }
}

// Usage
lblStatus.Text = status.GetDisplayName();
Enter fullscreen mode Exit fullscreen mode

Now:

public static class VisitStatusExtensions
{
    extension(VisitStatus status)
    {
        public string DisplayName => status switch
        {
            VisitStatus.PendingApproval => "Pending Approval",
            VisitStatus.CheckedIn => "Checked In",
            VisitStatus.CheckedOut => "Checked Out",
            _ => status.ToString()
        };
    }
}

// Usage
lblStatus.Text = status.DisplayName;
Enter fullscreen mode Exit fullscreen mode

It reads like the enum always had a DisplayName property. Extension blocks can also add static members and operators to types you don't own.

Over time, I moved away from enums for display text anyway. From my Android work, I liked keeping repeated strings in one place, so I started using a class of string properties instead. For example, one message for shift hours and another for after hours, defined once and reused everywhere.


6. Null-Conditional Assignment (C# 14)

Reading through a possibly null object was easy since C# 6, with ?.. But writing to one still needed a full if block.

Then:

if (visitor != null)
{
    visitor.CheckOutTime = DateTime.Now;
}
Enter fullscreen mode Exit fullscreen mode

Now:

visitor?.CheckOutTime = DateTime.Now;
Enter fullscreen mode Exit fullscreen mode

If visitor is null, nothing happens, and the right side isn't even evaluated. There's no separate ?.= operator. It's the same ?. you already know, now allowed on the left side of an assignment.


7. Labeled break and continue (C# 15)

This one arrived with .NET 11 just this month. When I needed to exit two nested loops at once, I had two options: a bool found flag checked at every level, or goto. I used goto. I honestly don't remember where anymore, and maybe that's for the best.

Then:

Visitor match = null;

foreach (var badge in scannedBadges)
{
    foreach (var visitor in expectedVisitors)
    {
        if (badge.Id == visitor.BadgeId)
        {
            match = visitor;
            goto Found;
        }
    }
}

Found:
Console.WriteLine(match?.FullName ?? "No match");
Enter fullscreen mode Exit fullscreen mode

Now:

Visitor? match = null;

outer: foreach (var badge in scannedBadges)
{
    foreach (var visitor in expectedVisitors)
    {
        if (badge.Id == visitor.BadgeId)
        {
            match = visitor;
            break outer;
        }
    }
}

Console.WriteLine(match?.FullName ?? "No match");
Enter fullscreen mode Exit fullscreen mode

The label sits on the loop, and break outer; jumps out of both loops. Visual Studio even has a new analyzer, IDE0410, that finds your old goto and flag patterns and offers to convert them.


Key Takeaways

  1. Most of these features don't make code faster. They remove noise, so the intent of the code is easier to see.
  2. Some pain becomes invisible through habit. Backing fields never felt wrong to me until field showed me what I'd been typing for years.
  3. Eleven years passed between C# 6 and C# 15, but four of these seven features landed in just the last two releases.

My verdict: if I could send only one of these back to 2016, it would be target-typed new(). It's the smallest change on this list, but I'd have used it hundreds of times a week. The one that surprised me most is field, because I never knew I wanted it.


Over to You

Which C# feature do you wish you'd had on day one? And what year did you start?

Drop it in the comments. One line is enough.

 

Thanks for reading! 🙏

If this helped, a reaction or a bookmark helps other C# developers find it too.

I publish practical .NET and C# posts every week. Follow me so you don't miss the next one.

Hiring a .NET developer? Email me at contact@umerfarooqofficial.com 📧

My portfolio and free .NET roadmap: https://umerfarooqofficial.com 🌐

Top comments (3)

Collapse
 
umerfarooqofficial profile image
Umer Farooq •

Hot take: when C# 9 dropped, I didn't like top-level statements. It felt like hiding structure from beginners. Now I get annoyed every time I open an older repo with a 15-line Program.cs wrapper.

Anyone else still prefer the explicit Main method?

Collapse
 
naheel0 profile image
Naheel Muhammed •

Mine would be required members and records. Almost every DTO I wrote by hand was a constructor plus null checks plus a mutable object nobody should've been able to change, and those two features deleted that whole category of code.
On #4 I'd push back slightly. For INotifyPropertyChanged, the field keyword is the language catching up to what source generators already solved. CommunityToolkit.Mvvm's [ObservableProperty] on a private field generates the same setter, equality check included, and it works on C# 12 today. field wins because there's no generator in the build, but on a WinForms or WPF codebase I'd reach for the generator first and treat field as what I use when I can't add a package.
What's on your list for someone starting in 2026 instead of 2016?

Collapse
 
umerfarooqofficial profile image
Umer Farooq •

Records (C# 9) and required members (C# 11) are great picks. Honestly, I should have included them for DTOs. Fair point on CommunityToolkit.Mvvm too, though I haven't used it much myself and would love to try it. Back in 2016, there were no source generators yet, which is why hand-written backing fields felt like the only way. Today, field is great when you just want zero extra dependencies. For someone starting in 2026, I'd tell them to turn on nullable reference types from day one and lean on pattern matching early.