At Trailhead, we think that .NET is the world’s most powerful and flexible framework for custom development. That’s why we are so excited that .NET 7 was officially released this week. When you see what’s new in .NET 7, we think you will agree that it moves the platform forward in some very compelling and useful ways.
There’s a lot that’s new, so I’ve outlined below just a few of the features that we’re most excited about using first.
C# 11 Updates
One of the biggest sets of changes in .NET 7 is the new features of C# 11. Below are a few that we think are particularly cool. If you’d like to know more about these or see a complete list, check out the list of new features from Microsoft.
File-Scoped Types
You can use the file access modifier to create a type whose visibility is scoped to just the source file in which it is declared. This can help you avoid naming collisions.
Generic Math Support
Generic math support is unlocked by the other new feature of C# 11, static abstract and virtual members, which might otherwise seem like a pretty obscure object-oriented programming (OOP) change.
You can read more about generic math in .NET 7 in this excellent blog post by Tanner Gooding from the Microsoft team.
Auto-Default Structs
All fields and auto-properties of a struct type are now automatically initialized to their default value if they are not set by the constructor.
UTF-8 String Literals
You can specify the u8 suffix on a string literal to specify UTF-8 character encoding.
byte[] u8Span = "ABC"u8;
u8Span.ToArray().Should().BeEquivalentTo(new[] { 65, 66, 67 });
Required Members
The required modifier indicates that a field or property must be initialized either by the constructor method or by an object initializer statement used when calling the constructor.
public class Person
{
public Person() { }
[SetsRequiredMembers]
public Person(string firstName, string lastName) =>
(FirstName, LastName) = (firstName, lastName);
public required string FirstName { get; init; }
public required string LastName { get; init; }
public int? Age { get; set; }
}
public class Student : Person
{
public Student() : base()
{
}
[SetsRequiredMembers]
public Student(string firstName, string lastName) :
base(firstName, lastName)
{
}
public double GPA { get; set; }
}
Raw String Literals
Raw string literals are a new format for string literals that can contain arbitrary text, including whitespace, new lines, embedded quotes, and other special characters without requiring escape sequences.
A raw string literal starts with three or more double-quotes and ends with a matching number of double-quotes. This can be great for JSON-formatted string data, as in the following example:
var sample = """
{
"PropertyA" : "Value"
}
""";
Generic Attributes
You can now create a generic class that inherits from System.Attribute. This feature provides a more convenient syntax for attributes that require a Type parameter.
// Before C# 11:
public class TypeAttribute : Attribute
{
public TypeAttribute(Type t) => ParamType = t;
public Type ParamType { get; }
}
[TypeAttribute(typeof(string))]
public string Method() => default;
Using this new feature, you can create a generic attribute instead:
// C# 11 feature:
public class GenericAttribute<T> : Attribute { }
Then, specify the type parameter to use the attribute:
[GenericAttribute<string>()]
public string Method() => default;
Newlines in String Interpolation Expressions
The text inside the { and } characters for an interpolated string can now include new-line characters, allowing you to write more complex, multi-line C# expressions in your interpolated strings.
List Patterns
The very powerful pattern matching feature in C# now includes array/list patterns.
public static int CheckSwitch(int[] values)
=> values switch
{
[1, 2, .., 10] => 1,
[1, 2] => 2,
[1, _] => 3,
[1, ..] => 4,
[..] => 50
};
WriteLine(CheckSwitch(new[] { 1, 2, 10 })); // prints 1
WriteLine(CheckSwitch(new[] { 1, 2, 7, 3, 3, 10 })); // prints 1
WriteLine(CheckSwitch(new[] { 1, 2 })); // prints 2
WriteLine(CheckSwitch(new[] { 1, 3 })); // prints 3
WriteLine(CheckSwitch(new[] { 1, 3, 5 })); // prints 4
WriteLine(CheckSwitch(new[] { 2, 5, 6, 7 })); // prints 50
.NET MAUI Updates
Thanks to .NET MAUI, the world of cross-platform app development is undergoing a major revolution right now. If you want to get caught up on what this evolution of Xamarin is all about, check out our blog post on it.
In .NET 7, MAUI now has several new features, including a new Map control, better support for dual screen devices, improvements to the UI responsiveness, faster startup, and better overall app size. Microsoft has also released a migration assistant for upgrading your Xamarin projects to .NET MAUI.
ASP.NET 7 Updates
With the web and web APIs still being at the center of so many modern applications, server-side ASP.NET is as important as ever. The ASP.NET team continues to make major investments into ASP.NET Core in .NET 7, including the handful of our favorite new features below:
Rate-Limiting Middleware
If you want to limit the traffic you get to a web application or API, you have a new option with rate-limiting middleware that controls the load on your application by queueing up traffic when it exceeds certain limits. This prevents other parts of your application from becoming a bottleneck.
MVC and Razor Pages
Microsoft hasn’t forgotten about server-side web applications, either. MVC and Razor pages got some improvements, like nullable models and a customized cookie consent value. You can read more about these changes on the Microsoft Learn site.
API Controllers
API controllers can now use the new decompression middleware to allow them to handle requests with compressed content. Controller classes also get better dependency injection support without the need to use the [FromServices] attribute.
Minimal APIs
We’re excited about Minimal APIs here at Trailhead. They are the new, low-code way to define web API endpoints in ASP.NET.
Minimal APIs get some big improvements in .NET 7, including the addition of filters. This allows you to run code before or after your route handlers, or to inspect and modify your requests or responses. Other changes make unit tests easier to write for minimal APIs, add better support for the OpenAPI standard (formerly known as Swagger), and make streaming and file uploads easier.
Finally, minimal APIs built in .NET 7 will have new ways of defining routes in groups that share the same base path or other common configuration settings.
gRPC
A new transcoder middleware allows you to write gRPC services that also operate as RESTful JSON APIs. This allows you to use the latest and greatest web service technologies while still providing backwards compatibility for clients who haven’t been upgraded to use gRPC.
Performance
Output caching is a new middleware in .NET 7 which stores responses from a web app and serves them from a cache.
HTTP/3 is also now fully supported, and Kestrel can handle HTTP/2 traffic much faster than it used to in .NET 6. There is even new support for running SignalR websockets over HTTP/2, something that we’ve sorely missed in previous versions.
You can read more about the many great performance improvements for ASP.NET Core in .NET 7 here.
Other
There is new and improved console output for dotnet watch now. The developer exception page in ASP.NET has a dark mode in .NET 7. If you prefer to use Program.Main instead of top-level statements, there’s a dotnet new command-line switch for that. Also, dotnet new includes new React and Angular starter templates, and the dotnet command-line interface includes new tooling for JWTs.
Cloud Native and Containers
Our .NET applications have been able to run in containers for a long time now, but with .NET 7, Microsoft has added support for creating containerized applications as part of a build publish process, with no need for an explicit Docker build phase.
With so many cool new features, we suspect many of you will be as eager as we are to incorporate .NET 7 into your projects.
ARM64 Support
These days ARM processors are getting more and more common because of their high performance and low power consumption. With this release, .NET now fully supports targeting the ARM architecture.
Conclusion
If you’re planning the upgrade to .NET 7, remember that Microsoft follows a tick-tock pattern with .NET versions. All even numbered versions are under long-term support (LTS) and all odd numbered versions are under standard-term support (STS), which is about 18 months.
Because .NET 7 is an odd version number, it is an STS release. You should only upgrade if you also plan to upgrade your code again in about a year, when .NET 8 comes.