.NET Celebrates 20 years and brings us a GIFT🎁: New .NET 7 Features Revealed
Tags: csharp, dotnet, programming, news
Published: 2022-04-04 11:25:00

Who better person to announce the 20th anniversary of .NET than Bill Gates? Well, that’s how it went at the [2002 VSLive!](https://visualstudiomagazine.com/articles/2022/02/16/birth-of-net.aspx)

It’s amazing! But in case you didn’t know, the **.NET framework**, according to [Stack Overflow survey](https://insights.stackoverflow.com/survey/2021#section-most-loved-dreaded-and-wanted-other-frameworks-and-libraries), has been **the most loved framework by developers for the past 3 years.**

Yes, the .NET Framework **celebrated 20 years since its creation** in 2002 and just a couple of days ago, Microsoft has released [new features of .NET 7](https://devblogs.microsoft.com/dotnet/announcing-dotnet-7-preview-2/#tab-completion), which is expected to be officially released in November this year. Let’s see what these features are and analyze them!

---

## Improved Regex source generator

The new **Regex Source Generator** means that you can shave off up to 5x the time spent optimizing patterns from our compiled engine **without having to sacrifice any of the associated performance benefits.** Additionally, because it operates within a `partial` class, there is no overhead of compiling-time for instances when users know their pattern at runtime. With this feature, if your pattern can be known at compile-time, then this generator is recommended instead of our traditionally compiler based approach.

All you need to do is set up a partial declaration with an attribute called `RegexGenerator` which points back to the method that will return an already precompiled regular expression object (with all other features enabled). Our generator will take care of creating that method and updating it as needed according to changes made to either the original string or passed in options (like case sensitivity etc…).

Let’s take a look at the comparison of the [Microsoft example](https://devblogs.microsoft.com/dotnet/announcing-dotnet-7-preview-2/#introducing-the-new-regex-source-generator):

**Before:**

```csharp
public class Foo
{
  public Regex regex = new Regex(@"abc|def", RegexOptions.IgnoreCase);

  public bool Bar(string input)
  {
    bool isMatch = regex.IsMatch(input);
    // ..
  }
}
```

**After:**

```csharp
public partial class Foo  // <-- Make the class a partial class
{
   [RegexGenerator(@"abc|def", RegexOptions.IgnoreCase)] // <-- Add the RegexGenerator attribute and pass in your pattern and options
  
   public static partial Regex MyRegex(); //  <-- Declare the partial method, which will be implemented by the source generator
  public bool Bar(string input)
  {
    bool isMatch = MyRegex().IsMatch(input); // <-- Use the generated engine by invoking the partial method.
    // ..
  }
}
```

----

## SDK improvements

For those who work with .NET Framework, using `dotnet new` will be a much easier task. With major updates such as an improved intuitiveness and an **increased speed of tab completion**, it would be hard to find anything negative about this change.

If you want to go deeper, here is the [original example from Microsoft:](https://devblogs.microsoft.com/dotnet/announcing-dotnet-7-preview-2/#sdk-improvements)

```csharp
❯ dotnet new --help
Description:
  Template Instantiation Commands for .NET CLI.

Usage:
  dotnet new [<template-short-name> [<template-args>...]] [options]
  dotnet new [command] [options]

Arguments:
  <template-short-name>  A short name of the template to create.
  <template-args>        Template specific options to use.

Options:
  -?, -h, --help  Show command line help.

Commands:
  install <package>       Installs a template package.
  uninstall <package>     Uninstalls a template package.
  update                  Checks the currently installed template packages for update, and install the updates.
  search <template-name>  Searches for the templates on NuGet.org.
  list <template-name>    Lists templates containing the specified template name. If no name is specified, lists all templates.
```

---

## New command names

Command lines in general were changing — specifically, every command shown in this output **will no longer include the** `--` **prefix** as it does now. This was done to align with what a user would expect from a subcommand for an app built for command lines.

The old versions of these commands (`--install`, etc) are still available just in case it breaks scripts but we hope that one day there will be deprecation warnings included to those commands so **you can transition over without risk**.

---

## Tab key completion

For a long time now, the dotnet command line interface has had support for tab completion on shells such as _PowerShell_, _bash_, _zsh_, and _fish_. The commands themselves are able to decide what they want to show when given input.

Now in .NET 7, the `new` command allows quite a lot of functionalities:

```csharp
❯ dotnet new angular
angular              grpc                 razor                viewstart            worker               -h
blazorserver         mstest               razorclasslib
web                  wpf                  /?
blazorwasm           mvc                  razorcomponent       webapi               wpfcustomcontrollib  /h
classlib             nugetconfig          react                webapp               wpflib               install
console              nunit                reactredux           webconfig            wpfusercontrollib    list
editorconfig         nunit-test           sln                  winforms             xunit                search
gitignore            page                 tool-manifest        winformscontrollib   --help               uninstall
globaljson           proto                viewimports          winformslib          -?                   update
```

If you are interested in knowing how to enable it, I recommend you to check the [Microsoft’s guide](https://docs.microsoft.com/dotnet/core/tools/enable-tab-autocomplete). And likewise, if you want to know all the possibilities of this feature, I recommend you to consult again the [original source](https://devblogs.microsoft.com/dotnet/announcing-dotnet-7-preview-2/#tab-completion).

---

## Dynamic PGO improvements

Microsoft has recently introduced a new breakthrough for program optimization. Called the Dynamic PGO, it is designed to make some key changes from the Static PGO we already know about. Where Static PGO requires developers to use special tools separately from training, Dynamic PGO doesn’t require any of that; all you need to do is run the application you are interested in optimizing and then collect data for Microsoft!

Now, according with [Andy Ayers](https://github.com/AndyAyersMS) in [GitHub](https://github.com/dotnet/runtime/pull/63720), the following is added:

Extend ref counting done by local morph so that we can determine
single-def single-use locals.

Add a phase that runs just after local morph that will attempt to
forward single-def single-use local defs to uses when they are in
adjacent statements.

Fix or work around issues uncovered elsewhere:

- `gtFoldExprCompare` might fold "identical" volatile subtrees.

- `fgGetStubAddrArg` cannot handle complex trees.

- Some simd/hw operations can lose struct handles.

- Some calls cannot handle struct local args.

You can check the [original pull](https://github.com/dotnet/runtime/pull/63720) for more details.

---

Conclusion

These features are the last ones that have come to light by Microsoft and we will have to see what more features they show us.

As far as we can all see (and as far as Microsoft lets us see), everything is going according to plan and **we can expect the official launch of .NET 7 at the end of this year** (more specifically in **November**).

So for the moment, we will be watching for any announcement or news from Microsoft on .NET 7!