C# 9.0: An Evolutionary Leap in Microsoft’s Programming Language

Posted by

In the world of software development, staying up-to-date with the latest advancements is crucial. Microsoft’s C# 9.0 is a testament to the continuous evolution of programming languages, offering developers a wide range of new features and capabilities. With its emphasis on modern workloads and improved productivity, C# 9.0 is set to revolutionize the way software applications and services are built. In this article, we will explore the top features of C# 9.0 and delve into the benefits they bring to developers.

Init-only properties

C# 9 allows you to define properties that can only be set during object initialization but cannot be modified afterward.

public class Person
{
public string Name { get; init; }
public int Age { get; init; }
}
// Usage
var person = new Person { Name = "John", Age = 30 };
person.Name = "Mike"; // Error: Cannot modify init-only property

Record types

One of the standout features of C# 9.0 is the introduction of record types. These types allow developers to encapsulate data with ease, offering a convenient way to define reference types. Record types can have both immutable and mutable properties, although they are primarily designed to support immutable data. They come with a range of useful features, including concise syntax for mutation, value equality, inbuilt formatting display, and support for inheritance hierarchies.

public record Point(int X, int Y);
// Usage
var p1 = new Point(2, 3);
var p2 = new Point(2, 3);
Console.WriteLine(p1 == p2); // true (value equality)

Pattern matching enhancements

Pattern matching has been extended to support additional patterns and improve expressiveness.

if (shape is Circle { Radius: > 0 } circle)
{
Console.WriteLine($"Circle with radius {circle.Radius}");
}
else if (shape is Rectangle { Width: var width, Height: var height })
{
Console.WriteLine($"Rectangle with width {width} and height {height}");
}

Top-level statements

C# 9 allows you to write code directly in the file without explicitly defining a class or method.

using System;
Console.WriteLine("Hello, world!");

Improved target-typed expressions:

C# 9 introduces better type inference, allowing you to omit explicit types in more scenarios.

List numbers = new(); // Equivalent to: List numbers = new List();
var result = numbers.Select(n => n * 2); // var infers the IEnumerable type

Function pointers:

Function pointers enable you to store and invoke functions dynamically.

delegate int BinaryOperation(int a, int b);
static int Add(int a, int b) => a + b;
static int Subtract(int a, int b) => a - b;
BinaryOperation op = Add;
Console.WriteLine(op(5, 3)); // 8
op = Subtract;
Console.WriteLine(op(5, 3)); // 2

Native-sized integers:

C# 9 introduces the nint and nuint types, which are platform-specific and have the same size as a native pointer.

nint index = 42;
nuint offset = 100;

These are just some of the features introduced in C# 9. Each feature aims to improve code expressiveness, safety, and efficiency, providing developers with more powerful tools for building robust applications.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.