Thursday, 6 January 2022

Implicit usings in .NET MAUI

.NET MAUI Preview 11 now uses implicit usings to reduce the number of using statements you need to specify at the top of each file. For more information about implicit usings, see this blog post.

Specifically, from Preview 11 onwards you don’t need to add using statements for the following namespaces, which are all now available implicitly in .NET MAUI projects:

  • Microsoft.Extensions.DependencyInjection
  • Microsoft.Maui
  • Microsoft.Maui.Controls
  • Microsoft.Maui.Controls.Hosting
  • Microsoft.Maui.Controls.Xaml
  • Microsoft.Maui.Graphics
  • Microsoft.Maui.Essentials
  • Microsoft.Maui.Hosting

If you’re new to .NET MAUI, implicit usings really make life easier as you don’t have to hunt around to find out which namespaces specific types are in. However, note that there are sometimes types you’ll need to use that do reside in other namespaces for which you’ll have to add using statements (e.g. the types in Microsoft.Maui.Layouts).

The project templates also now use file-scoped namespaces. All I’ll say is it’s a syntax I’m still getting used to.

.NET Android

.NET Android projects now include the following implicit usings:

  • Android.App
  • Android.Widget
  • Android.OS.Bundle

Therefore, it’s not necessary to add using statements for the above namespaces.

.NET iOS

.NET iOS projects (and MacCatalyst, and tvOS) now include the following implicit usings:

  • CoreGraphics
  • Foundation
  • UIKit

Therefore, it’s not necessary to add using statements for the above namespaces.

.NET macOS

.NET macOS projects now include the following implicit usings:

  • AppKit
  • CoreGraphics
  • Foundation

Therefore, it’s not necessary to add using statements for the above namespaces.