It hasn’t been long, and we already have .NET 9. Let’s talk about new interesting features.
How to test C# 13 (.NET 9)?
Normally, when we try to build application
We will get an error
Error messageerror CS8652: The feature 'implicit indexer initializer' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
Following error message we have to change .csproj file
Python provides elements from the end of a collection by using negative indices (-1, -2, etc.). In C#, we can do the same by using the “^” operator, but until now we weren’t able to use it in initializer expression.
I don’t know of any case where I would use it, but now this limitation is gone.
Enhancements to Extensions
Impossible to testOn day 31.05.2024, this feature is still in progress, and it's not implemented in .NET 9.
The code
var square1 = new Square {
A = 2
};
Console.WriteLine(square1.CalculateArea());
Console.WriteLine(square1.CalculateDiagonal());
class Square {
public int A { get; set; }
}
static class TestExtensions {
public static int CalculateArea(this Square square) {
return square.A * square.A;
}
public static double CalculateDiagonal(this Square square) {
return square.A * Math.Sqrt(2);
}
}
Above is a simple example of existing extensions. What improvements can we make in .NET 9 and C# 13?
The CalculateArea method is good, and we have nothing to change here.
The CalculateDiagonal method would be clearer and more intuitive if it were a property, not a method. With C# 13 we can do that!
The code
var square1 = new Square {
A = 2
};
Console.WriteLine(square1.CalculateArea());
Console.WriteLine(square1.CalculateDiagonal());
Console.WriteLine(square1.Diagonal);
public class Square
{
public int A { get; set; }
}
public implicit extension SquareExtensions for Square
{
public double Diagonal => this.A * Math.Sqrt(2);
}
static class TestExtensions
{
public static int CalculateArea(this Square square) {
return square.A * square.A;
}
public static double CalculateDiagonal(this Square square) {
return square.A * Math.Sqrt(2);
}
}
This part of code extends the Square class with an attribute called Diagonal. What’s interesting is that we can extend everything, even types not owned (for example, from NuGet packages).
public implicit extension SquareExtensions for Square
{
public double Diagonal => this.A * Math.Sqrt(2);
}
Discover What’s New in C# 13: Exciting Features and Enhancements
.NET 9 was released on May 21, 2024.
.NET 9 was released on May 21, 2024.
A while ago, we talked about the new features introduced in .NET 8 – a worthy successor to .NET 6?
It hasn’t been long, and we already have .NET 9. Let’s talk about new interesting features.
How to test C# 13 (.NET 9)?
Normally, when we try to build application
We will get an error
Following error message we have to change .csproj file
Original .csproj file
Final version
After this modification, the application builds successfully.
Implicit index access
This feature is useful when we are using arrays with a known size.
The code
Until now, we could do something like this.
But now we are available to do something like this
Python provides elements from the end of a collection by using negative indices (-1, -2, etc.). In C#, we can do the same by using the “^” operator, but until now we weren’t able to use it in initializer expression.
I don’t know of any case where I would use it, but now this limitation is gone.
Enhancements to Extensions
The code
Above is a simple example of existing extensions. What improvements can we make in .NET 9 and C# 13?
The CalculateArea method is good, and we have nothing to change here.
The CalculateDiagonal method would be clearer and more intuitive if it were a property, not a method. With C# 13 we can do that!
The code
This part of code extends the Square class with an attribute called Diagonal. What’s interesting is that we can extend everything, even types not owned (for example, from NuGet packages).
You can track the progress on Language Feature Status.
You can read more about C# 13 at: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13
Archives
Understanding K-Nearest-Neighbors (KNN) – Essential Machine Learning Algorithm
2024-10-06A Step-by-Step Guide to Web Scraping for Beginners
2024-07-12Categories
Meta