Exploring the Latest C# Features
C# continues to evolve, introducing powerful features that enhance productivity, performance, and
maintainability. This document explores the latest C# features, demonstrating how they improve
upon previous versions .
1. Primary Constructors
Before C# 12 (Explicit Constructor Definition)
public class Person
private string _name;
private int _age;
public Person(string name, int age)
_name = name;
_age = age;
public void Display() => Console.WriteLine($"Name: {_name}, Age: {_age}");
After C# 12 (Using Primary Constructor)
public class Person(string name, int age)
public void Display() => Console.WriteLine($"Name: {name}, Age: {age}");
Benefit: Less boilerplate code, improved readability.
2. File-Local Types
Before C# 12 (No Restriction on Type Scope)
internal class HelperClass
public static void Assist() => Console.WriteLine("Helper method.");
}
After C# 12 (File-Local Types)
file class HelperClass
public static void Assist() => Console.WriteLine("Helper method.");
Benefit: Limits the scope of helper classes to a single file, preventing unintended external usage.
3. Default Parameters in Lambda Expressions
Before C# 12 (Not Allowed)
Func<string, string> greet = (name) => $"Hello, {name}!";
Console.WriteLine(greet("Alice"));
After C# 12 (With Default Parameter Support)
Func<string, string> greet = (string name = "Guest") => $"Hello, {name}!";
Console.WriteLine(greet()); // Output: Hello, Guest!
Benefit: Allows default values for lambda parameters, making code more flexible.
4. Span and Memory Enhancements
Before C# 12 (Heap Allocation for Strings)
char[] chars = new char[] { 'H', 'e', 'l', 'l', 'o' };
string str = new string(chars);
Console.WriteLine(str);
After C# 12 (Using `` for Stack Allocation)
Span<char> span = stackalloc char[5] { 'H', 'e', 'l', 'l', 'o' };
Console.WriteLine(span.ToString());
Benefit: Reduces memory allocation, improving performance for high-speed applications.
5. Optimized String Interpolation
Before C# 12 (Standard Interpolation)
string name = "Alice";
int age = 25;
string message = $"Name: {name}, Age: {age}";
After C# 12 (Optimized Interpolation for Performance)
string message = $"Name: {name}, Age: {age}".AsSpan().ToString();
Benefit: Optimizes string interpolation performance, reducing unnecessary allocations.
6. GitHub Copilot Integration
GitHub Copilot is an AI-powered code completion tool that assists developers by suggesting entire
lines or blocks of code based on the context of their work.
Summary: C# 12 Feature Improvements
Feature Before C# 12 After C# 12 Benefit
Parameters are part of the Less boilerplate
Primary Constructors Requires explicit constructor
class definition code
Internal classes visible across Better
File-Local Types Limited to a single file
the assembly encapsulation
Default Parameters in More flexible
Not supported Supported
Lambdas code
Better
Span** & Memory** Heap allocation Stack allocation
performance
Optimized String Optimized for memory
Standard interpolation Faster execution
Interpolation usage
Minimal API Default parameters not Default parameters
More concise APIs
Enhancements supported supported
Final Thoughts
C# 12 brings improvements that reduce boilerplate code, enhance performance, and make the
language more expressive. Whether you're optimizing performance-critical applications or
simplifying API definitions, these features provide valuable enhancements for modern C#
development.