CategoriesSyntax Sugar

Syntax Sugar

8 features across 6 C# versions

C# 2.02005

Iterators and yield return

Simplifies custom iteration definitions by generating a compact state-machine class under the hood for lazy loading loops.

1public IEnumerable<int> GetNumbers() {
2 yield return 1;
3 yield return 2;
4}
C# 62015

Null-Conditional Operator (?.)

Halts object branch evaluation safely if an entry along the chain references a null target state.

1string zip = customer?.Profile?.Address?.ZipCode;
C# 62015

String Interpolation

Replaces traditional visual text placeholders with readable inline contextual tracking parameters.

1string overview = $"User: {user.Name} holds id: {user.Id}";
C# 82019

Switch Expression Assignments

Transforms bulky procedural switch logic loops into concise mathematical expression routing statements.

1int priority = level switch { "Admin" => 1, "User" => 2, _ => 9 };
C# 102021

Global Usings Namespace Tracking

Broadly references targeted dependency rules across entire compilation layers from an isolated central definitions point.

1global using System.Net.Http;
2global using System.Text.Json;
C# 102021

File-Scoped Namespace Definitions

Eliminates unnecessary brace indentation blocks for general file scopes by declaring names across single instruction boundaries.

1namespace Engine.Core.Processor;
2public class Worker { }
C# 112022

Raw String Literals

Captures multi-line configuration blocks containing structural quotes completely free from manual character escaping logic loops.

1string query = """
2{
3 "queryType": "AllRecords",
4 "limit": 50
5}
6""";
C# 122023

Unified Collection Expressions

Provides an elegant, bracketed syntax layout to construct lists, spans, or arrays without verbose constructor declarations.

1int[] row = [1, 2, 3, 4];
2Span<char> alphabet = ['a', 'b', 'c'];