.NET 6 Features – Part 2

.Net 6 Features

This is the second part of our series on new features in Microsoft’s .NET 6 update. Inside, we discuss new additions to the popular programming language and platform, including how to work with file-scoped namespaces, directives and implicit global, setting application wide default fonts, and more.

.NET 6 Features and Updates

Before we begin, feel free to read the first part in this two-part series: .NET 6 Features and Updates.

File-scoped Namespaces in .NET

With file-scoped namespaces, we can declare the namespace for an entire file instead of nesting the remaining contents in brackets { … }. This reduces indentation tremendously.

Here is the usual syntax:

namespace MyNamespace;
class MyClass { ... } 

Here is the newer syntax in .NET 6:

namespace MyNamespace
{
    class MyClass { ... } 
}

Implicit Global Using Directives

Implicit global using directives enables us to specify using directives once and have them applied to every file that gets compiled. We can also use types defined in these namespaces without having to specify their fully qualified names.

Global using directives can be added to any source file – the best practice however is to place all of them in a single file.

Examples of the global using statement in .NET 6 follows:

global using System;
global using static System.Math;

What is interesting here is that the global modifier can be combined with the static modifier. In the above example all the methods declared in System.Math are enabled in all files in your project.

The global modifier can also be applied to a using alias directive for example:

global using Env = System.Environment;

Application-wide Default Font in .NET 6

We have all become accustomed to having Microsoft Sans Serif or Segoe UI as a default font on Windows forms. It was a bit tedious to get all the fonts and controls the same. This is simplified in .NET 6, as you can see in the following code example:

class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);
        Application.SetDefaultFont(new Font(new FontFamily("Microsoft Sans Serif"), 8f));
        Application.Run(new Form1());
    }
}

Especially important to note is the fact that the Application.SetDefaultFont method should be called before the first window is created, as in the above example.

.NET 6 Target Framework Moniker

To target .NET 6 and get access to all the cross-platform APIs that .NET has to offer, make use of the net6.0 Target Framework Moniker.

For console apps, cross-platform libraries, and ASP.NET Coreapps, use the following:

net6.0

For specific operating systems, such as Windows Forms or iOS, make use of the following:

  • net6.0-android
  • net6.0-ios
  • net6.0-maccatalyst
  • net6.0-tvos
  • net6.0-windows

Notice that the above TFMs are versionless; this is because each of them is targeting the lowest supported system.

Read: Working with Files and Directories in .NET

More by Author

Must Read