Resolve warnings related to language features and versions

This article covers the following compiler warnings:

  • CS8022, CS8023, CS8024, CS8025, CS8026, CS8059, CS8107, CS8302, CS8320, CS8370, CS8400, CS8773, CS8936, CS9058: Feature is not available. Use newer language version.
  • CS8058: Feature is experimental.
  • CS8192: Provided language version is unsupported or invalid
  • CS8303: Specified language version cannot have leading zeroes
  • CS8304: Compiler version is less than language version
  • CS1738: Named argument specifications must appear after all fixed arguments have been specified.
  • CS8306: Tuple element name is inferred.
  • CS8314: An expression of type cannot be handled by a pattern of type
  • CS8371: Field-targeted attributes on auto-properties are not supported in language version
  • CS8401: To use @$ instead of $@ for an interpolated verbatim string, use newer language version.
  • CS8511: An expression of type cannot be handled by a pattern of type.
  • CS8627: A nullable type parameter must be known to be a value type or non-nullable reference type
  • CS8630: Invalid nullable options. Use newer language version
  • CS8652: The modifier is not valid for this item.
  • CS8704: Type does not implement interface member. It cannot implicitly implement a non-public member.
  • CS8706: Type cannot implement interface member because a feature is not available in this version.
  • CS8904: Invalid variance: The type parameter must be valid.
  • CS8912: Inheriting from a record with a sealed 'Object.ToString' is not supported.
  • CS8919: Cannot implement specified interface member in type because the target runtime doesn't support static abstract members in interfaces
  • CS8929: Method cannot implement interface member in type because the target runtime doesn't support static abstract members in interfaces.
  • CS8957: Conditional expression is not valid in language version because a common type was not found between types.
  • CS8967: Newlines inside a non-verbatim interpolated string are not supported in C#
  • CS9014: Error: Use of possibly unassigned property. Upgrade to auto-default the property.
  • CS9015: Error: Use of possibly unassigned field. Upgrade to auto-default the field.
  • CS9016: Warning: Use of possibly unassigned property. Upgrade to auto-default the property.
  • CS9017: Warning: Use of possibly unassigned field. Upgrade to auto-default the field.
  • CS9064: Target runtime doesn't support ref fields.
  • CS9103: Definition in a module with an unrecognized RefSafetyRulesAttribute version, expecting '11'.
  • CS9171: Target runtime doesn't support inline array types.
  • CS9194: Argument may not be passed with the ref keyword. To pass ref arguments to in parameters, upgrade to language version 12 or greater.
  • CS9202: Feature is not available in C# 12.0. Please use newer language version
  • CS9211: The diagnosticId argument to the 'Experimental' attribute must be a valid identifier.

In addition, the following errors and warnings relate to struct initialization changes in recent versions:

  • CS0171, CS8881: Backing field for automatically implemented property 'name' must be fully assigned before control is returned to the caller.
  • CS0188, CS8885: The 'this' object cannot be used before all of its fields are assigned to
  • CS0843, CS8880: Backing field for automatically implemented property 'name' must be fully assigned before control is returned to the caller
  • CS8305: Feature is for evaluation purposes only and is subject to change or removal in future updates.
  • CS9204: Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.

The cause behind all these errors and warnings is that the compiler installed supports a newer version of C# than the version your project has selected. The C# compiler can conform to any previous version. You can validate syntax against an earlier version of C#, or because your project must support older libraries or runtimes.

There are two possible causes and three ways to address these errors and warnings.

Update your target framework

The compiler determines a default based on these rules:

Target Version C# language version default
.NET 8.x C# 12
.NET 7.x C# 11
.NET 6.x C# 10
.NET 5.x C# 9.0
.NET Core 3.x C# 8.0
.NET Core 2.x C# 7.3
.NET Standard 2.1 C# 8.0
.NET Standard 2.0 C# 7.3
.NET Standard 1.x C# 7.3
.NET Framework all C# 7.3

If your selected framework doesn't match the language version required, you can upgrade the target framework.

Select the matching language version

You might have an older target framework selected in your project file. If you remove the LangVersion element from your project file, the compiler uses the default value listed in the preceding section. The following table shows all current C# language versions. You can also specify a specific language version to enable newer features.

Value Meaning
preview The compiler accepts all valid language syntax from the latest preview version.
latest The compiler accepts syntax from the latest released version of the compiler (including minor version).
latestMajor
or default
The compiler accepts syntax from the latest released major version of the compiler.
12.0 The compiler accepts only syntax that is included in C# 12 or lower.
11.0 The compiler accepts only syntax that is included in C# 11 or lower.
10.0 The compiler accepts only syntax that is included in C# 10 or lower.
9.0 The compiler accepts only syntax that is included in C# 9 or lower.
8.0 The compiler accepts only syntax that is included in C# 8.0 or lower.
7.3 The compiler accepts only syntax that is included in C# 7.3 or lower.
7.2 The compiler accepts only syntax that is included in C# 7.2 or lower.
7.1 The compiler accepts only syntax that is included in C# 7.1 or lower.
7 The compiler accepts only syntax that is included in C# 7.0 or lower.
6 The compiler accepts only syntax that is included in C# 6.0 or lower.
5 The compiler accepts only syntax that is included in C# 5.0 or lower.
4 The compiler accepts only syntax that is included in C# 4.0 or lower.
3 The compiler accepts only syntax that is included in C# 3.0 or lower.
ISO-2
or 2
The compiler accepts only syntax that is included in ISO/IEC 23270:2006 C# (2.0).
ISO-1
or 1
The compiler accepts only syntax that is included in ISO/IEC 23270:2003 C# (1.0/1.2).

You can learn more about the language versions supported for each framework version in the article on Configure language version in the language reference section.

Avoid the updated feature

If you must support older libraries or runtimes, you might need to avoid using newer features.

Enable experimental features

The diagnostics for experimental features can be disabled to use the experimental feature.

Warning

Experimental features are subject to changes. The APIs may change, or they may be removed in future updates. Including experimental features is a way for library authors to get feedback on ideas and concepts for future development. Use extreme caution when using any feature marked as experimental.

You can also declare your own experimental features using the System.Diagnostics.CodeAnalysis.ExperimentalAttribute. The compiler emits CS9211 if the identifier used for the experimental feature is not a valid identifier.

Breaking changes on struct initialization

All these errors and warnings help ensure that struct types are properly initialized before their fields are accessed. In earlier versions of C#, you must explicitly assign all fields in a struct in any constructor. The parameterless constructor initializes all fields to their default value. In later versions, all constructors initialize all fields. Either the field is explicitly set, set in a field initializer, or set to its default value.

  • CS0171, CS8881: Backing field for automatically implemented property 'name' must be fully assigned before control is returned to the caller.
  • CS0188, CS8885: The 'this' object cannot be used before all of its fields are assigned to
  • CS0843, CS8880: Backing field for automatically implemented property 'name' must be fully assigned before control is returned to the caller

You can address this error by upgrading your language version to C# 11, every struct constructor initializes all fields. If that's not a possible option, you must explicitly call the default constructor, as shown in the following example:

struct S
{
    public int AIProp { get; set; }
    public S(int i){} //CS0843
    // Try the following lines instead.
    // public S(int i) : this()
    // {
    //     AIProp = i;
    // }
}

class Test
{
    static int Main()
    {
        return 1;
    }
}