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");
        }
    }
}
πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung

Now:

Console.WriteLine("Generating daily report");
πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung

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>>();
πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung

Now:

private Dictionary<int, List<Visitor>> _visitorsByDay = new();
πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung

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";
πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung

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;
πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung

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));
    }
}
πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung

Now:

public string VisitorName
{
    get;
    set
    {
        if (field == value) return;
        field = value;
        OnPropertyChanged(nameof(VisitorName));
    }
} = "";
πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung

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();
πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung

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;
πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung

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;
}
πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung

Now:

visitor?.CheckOutTime = DateTime.Now;
πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung

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");
πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung

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");
πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung

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 (1)

πŸ”΄ Live TV: umerfarooqofficial/c features i wish i had when i started in bel - Jadwal Siaran Langsung
 
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?