What’s New in .NET 7?

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.

Picture of J. Tower

J. Tower

Jonathan, or J as he's known to friends, is a husband, a father, and the co-owner of Trailhead Technology Partners. He is also an 8-time Microsoft MVP in .NET and frequently speaks at software meetups and conferences around the country and the world. He doesn't mind too much because he loves sharing what he’s learned, and it also gives him an excuse to visit any nearby National Parks, a passion of his, proven by the fact that he's currently made it to 52 of the 63 parks. J. also has a passion for building community and has served on several non-profit boards over the years as a result. Currently, J. sits on the SoftwareGR board, a non-profit trade organization dedicated to building the software industry in West Michigan. He also runs Beer City Code, a software conference, and has served as president on that board for over a decade. J. loves hiking, reading, music, photography, and trying to see all the best picture nominees before the Oscars ceremony.

Related Blog Posts

We hope you’ve found this to be helpful and are walking away with some new, useful insights. If you want to learn more, here are a couple of related articles that others also usually find to be interesting:

Our Gear Is Packed and We're Excited to Explore With You

Ready to come with us? 

Together, we can map your company’s software journey and start down the right trails. If you’re set to take the first step, simply fill out our contact form. We’ll be in touch quickly – and you’ll have a partner who is ready to help your company take the next step on its software journey. 

We can’t wait to hear from you! 

Main Contact

This field is for validation purposes and should be left unchanged.

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the form below. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

Montage Portal

Montage Furniture Services provides furniture protection plans and claims processing services to a wide selection of furniture retailers and consumers.

Project Background

Montage was looking to build a new web portal for both Retailers and Consumers, which would integrate with Dynamics CRM and other legacy systems. The portal needed to be multi tenant and support branding and configuration for different Retailers. Trailhead architected the new Montage Platform, including the Portal and all of it’s back end integrations, did the UI/UX and then delivered the new system, along with enhancements to DevOps and processes.

Logistics

We’ve logged countless miles exploring the tech world. In doing so, we gained the experience that enables us to deliver your unique software and systems architecture needs. Our team of seasoned tech vets can provide you with:

Custom App and Software Development

We collaborate with you throughout the entire process because your customized tech should fit your needs, not just those of other clients.

Cloud and Mobile Applications

The modern world demands versatile technology, and this is exactly what your mobile and cloud-based apps will give you.

User Experience and Interface (UX/UI) Design

We want your end users to have optimal experiences with tech that is highly intuitive and responsive.

DevOps

This combination of Agile software development and IT operations provides you with high-quality software at reduced cost, time, and risk.

Trailhead stepped into a challenging project – building our new web architecture and redeveloping our portals at the same time the business was migrating from a legacy system to our new CRM solution. They were able to not only significantly improve our web development architecture but our development and deployment processes as well as the functionality and performance of our portals. The feedback from customers has been overwhelmingly positive. Trailhead has proven themselves to be a valuable partner.

– BOB DOERKSEN, Vice President of Technology Services
at Montage Furniture Services

Technologies Used

When you hit the trails, it is essential to bring appropriate gear. The same holds true for your digital technology needs. That’s why Trailhead builds custom solutions on trusted platforms like .NET, Angular, React, and Xamarin.

Expertise

We partner with businesses who need intuitive custom software, responsive mobile applications, and advanced cloud technologies. And our extensive experience in the tech field allows us to help you map out the right path for all your digital technology needs.

  • Project Management
  • Architecture
  • Web App Development
  • Cloud Development
  • DevOps
  • Process Improvements
  • Legacy System Integration
  • UI Design
  • Manual QA
  • Back end/API/Database development

We partner with businesses who need intuitive custom software, responsive mobile applications, and advanced cloud technologies. And our extensive experience in the tech field allows us to help you map out the right path for all your digital technology needs.

Our Gear Is Packed and We're Excited to Explore with You

Ready to come with us? 

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the contact form. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

Thank you for reaching out.

You’ll be getting an email from our team shortly. If you need immediate assistance, please call (616) 371-1037.