Skip to main content

net 8 and c# 12 big change

· 6 min read

Net8

. NET 8, was released on November 14, 2023, along with C# 12 and Visual Studio 17.8. .NET 8 will be supported for three years as a long-term support (LTS) release. You can download .NET 8 here.

The .NET 8 runtime includes improvements to performance, garbage collection, and the core and extension libraries.

  • Some update summaries:

    • The .NET 8 runtime includes improvements to performance, garbage collection, and the core and extension libraries.

    • ASP.NET Core includes improvements to Blazor, SignalR, minimal APIs, Native AOT, Kestrel and HTTP.sys servers, and authentication and authorization.

    • .NET MAUI includes new functionality for controls, gesture recognizers, Windows apps, navigation, and platform integration.

    • Entity Framework Core includes improvements to complex type objects, collections of primitive types, JSON column mapping, raw SQL queries, lazy loading, tracked-entity access, model building, math translations, and other features.

    • Windows Forms includes improvements to data binding, Visual Studio DPI, and high DPI.

    • Windows Presentation Foundation (WPF) adds the ability to use hardware acceleration and a new OpenFolderDialog control.


C# 12

C# 12 is supported on .NET 8. C# 12 includes the following new features.

Primary constructors provide a concise syntax for initializing properties in C# classes. They allow you to declare and initialize properties directly in the constructor parameter list, reducing boilerplate code.

public class Person(string _name)
{
public string Name = _name;
}
Person person = new Person("John");
Console.WriteLine(person.Name); // Output: John
  • old style:
public class Person
{
public string Name { get; }
public int Age { get; }

public Person(string name, int age)
{
Name = name;
Age = age;
}
}

Collection expressions introduce a new terse syntax to create common collection values. Inlining other collections into these values is possible using a spread operator ...

The following examples show uses of collection expressions:

// Create an array:
int[] a = [1, 2, 3, 4, 5, 6, 7, 8];

// Create a list:
List<string> b = ["one", "two", "three"];

// Create a span
Span<char> c = ['a', 'b', 'c', 'd', 'e', 'f', 'h', 'i'];

// Create a jagged 2D array:
int[][] twoD = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];

// Create a jagged 2D array from variables:
int[] row0 = [1, 2, 3];
int[] row1 = [4, 5, 6];
int[] row2 = [7, 8, 9];
int[][] twoDFromVariables = [row0, row1, row2];

The spread operator, .. in a collection expression replaces its argument with the elements from that collection. The argument must be a collection type. The following examples show how the spread operator works:

int[] row0 = [1, 2, 3];
int[] row1 = [4, 5, 6];
int[] row2 = [7, 8, 9];
int[] single = [.. row0, .. row1, .. row2];
foreach (var element in single)
{
Console.Write($"{element}, ");
}
// output:
// 1, 2, 3, 4, 5, 6, 7, 8, 9,
  • Inline arrays - Introduced in Visual Studio 2022 version 17.7 Preview 3.

Inline arrays enable a developer to create an array of fixed size in a struct type

An inline array is declared similar to the following struct:

[System.Runtime.CompilerServices.InlineArray(10)]
public struct Buffer
{
private int _element0;
}

You use them like any other array:

var buffer = new Buffer();
for (int i = 0; i < 10; i++)
{
buffer[i] = i;
}

foreach (var i in buffer)
{
Console.WriteLine(i);
}

You can now define default values for parameters on lambda expressions. The syntax and rules are the same as adding default values for arguments to any method or local function.

// Define a lambda expression with default parameters
Func<int, int, int> add = (x, y = 0) => x + y;

// Call the lambda expression with one argument
int result1 = add(5);
Console.WriteLine("Result1: " + result1); // Output: 5

// Call the lambda expression with two arguments
int result2 = add(5, 3);
Console.WriteLine("Result2: " + result2); // Output: 8

pass parameters by reference while ensuring that the referenced value cannot be modified within the method. ref readonly parameters enables more clarity for APIs

int[] numbers = { 1, 2, 3, 4, 5 };
int sum = Plus(numbers);
Console.WriteLine("Sum: " + sum);

public static int Plus(ref readonly int[] arr)
{
int sum = 0;
foreach (int num in arr)
{
sum += num;
}
return sum;
}
  • Alias any type - Introduced in Visual Studio 2022 version 17.6 Preview 3.

use the using alias directive to alias any type, not just named types. That means you can create semantic aliases for tuple types, array types, pointer types, or other unsafe types.

Pair pair = (1, "apple");
int number = pair.Item1;
string fruit = pair.Item2;
Console.WriteLine($"Number: {number}, Fruit: {fruit}");

// using alias with tuple types:
using Pair = (int, string);

The System.Diagnostics.CodeAnalysis.ExperimentalAttribute allows developers to mark types, methods, or assemblies as experimental features. This attribute serves as a way to communicate to other developers that the marked feature is still under development or testing, and its API or behavior may change in future releases.

[Experimental("Experimental feature: FeatureName")]
public static void ExperimentalMethod()
{
// Experimental feature implementation
}

public static void Main()
{
// Call the experimental method
ExperimentalMethod();
}
  • Interceptors - Preview feature Introduced in Visual Studio 2022 version 17.7 Preview 3.

Interceptors are an experimental feature, available in preview mode with C# 12.

Risk upgrading from .net6 to .net 8

  • Compatibility: third-party libraries Some libraries or packages may not yet support the latest version,
  • development team may need time to familiarize themselves with the new features and changes introduced in .NET 8
  • update code, refactor components, and test thoroughly to ensure everything works as expected.
  • current version ready proxy c#12