Migrating from .NET Framework 4 to .NET 6

29 November 2021

This is a summary of a Microsoft video.

Preparation

Two tools:

Personal view on some style issues

Visual Studio is keen on generating lots of warnings and messages; here are some of the most popular ones and a suggestion on how to handle them:

Best practice everywhere

Message IDE1006: Naming rule violation: These words must begin with upper case characters: xxx Fix all private method names, or add IDE1006 to the projects list of suppressed warnings
Warning CA2213: 'xxx' contains field 'yyy' that is of IDisposable type 'zzz', but it is never disposed. Change the Dispose method on 'xxx' to call Close or Dispose on this field Apply the suggested fix, or add CA2213 to the projects list of suppressed warnings
Warning CA1001: Type 'xxx' owns disposable field 'yyy' but is not disposable For simple types, make them sealed and make them implement IDisposable, hence give them a Dispose() method
Warning CA1060: Move pinvokes to native methods class Yes, create a class named NativeMethods and put all DllImported methods there (make them 'public static extern')
Warning CA2101: Specify marshaling for P/Invoke string arguments Yes, specify the charset used; most often add "CharSet = CharSet.Unicode" as a DllImport parameter

Dotnet specific, i.e. different from .NET Framework

Warning CA1416: This call site is reachable on all platforms. 'xxx' is only supported on: 'windows' Add CA1416 to the projects list of suppressed warnings

Recent language extensions

At this point in time I prefer to keep the bulk of my code .NET Framework compatible, so a lot of promotional messages will be suppressed:

Message IDE0090: 'new' expression can be simplified Add IDE0090 to the projects list of suppressed warnings
Message IDE0017: Object initialization can be simplified Add IDE0017 to the projects list of suppressed warnings
Message IDE0057: Substring can be simplified Add IDE0057 to the projects list of suppressed warnings