|
| 1 | +--- |
| 2 | +title: Resolve errors related to language version and features |
| 3 | +description: Several compiler errors indicate that your configured language version doesn't support a feature you're using. This article shows how to fix those errors and warnings. |
| 4 | +f1_keywords: |
| 5 | + - "CS0171" |
| 6 | + - "CS0188" |
| 7 | + - "CS0843" |
| 8 | + - "CS8904" # ERR_UnexpectedVarianceStaticMember Invalid variance: The type parameter '{1}' must be {3} valid on '{0}' unless language version '{4}' or greater is used. '{1}' is {2}. |
| 9 | + - "CS1738" # ERR_NamedArgumentSpecificationBeforeFixedArgument Named argument specifications must appear after all fixed arguments have been specified. Please use language version {0} or greater to allow non-trailing named arguments |
| 10 | + - "CS8022" # ERR_FeatureNotAvailableInVersion1 Feature is not available in C# 1. Please use language version. |
| 11 | + - "CS8023" # ERR_FeatureNotAvailableInVersion2 Feature is not available in C# 2. Please use language version. |
| 12 | + - "CS8024" # ERR_FeatureNotAvailableInVersion3 Feature is not available in C# 3. Please use language version. |
| 13 | + - "CS8025" # ERR_FeatureNotAvailableInVersion4 Feature is not available in C# 4. Please use language version. |
| 14 | + - "CS8026" # ERR_FeatureNotAvailableInVersion5 Feature is not available in C# 5. Please use language version. |
| 15 | + - "CS8059" # ERR_FeatureNotAvailableInVersion6 Feature is not available in C# 6. Please use language version. |
| 16 | + - "CS8107" # ERR_FeatureNotAvailableInVersion7 Feature is not available in C# 7. Please use language version. |
| 17 | + - "CS8302" # ERR_FeatureNotAvailableInVersion7_1 Feature is not available in C# 7.1. Please use language version. |
| 18 | + - "CS8320" # ERR_FeatureNotAvailableInVersion7_2 Feature is not available in C# 7.2. Please use language version. |
| 19 | + - "CS8370" # ERR_FeatureNotAvailableInVersion7_3 Feature is not available in C# 7.3. Please use language version. |
| 20 | + - "CS8400" # ERR_FeatureNotAvailableInVersion8 Feature is not available in C# 8. Please use language version. |
| 21 | + - "CS8773" # ERR_FeatureNotAvailableInVersion9 Feature is not available in C# 9. Please use language version. |
| 22 | + - "CS8936" # ERR_FeatureNotAvailableInVersion10 Feature is not available in C# 10. Please use language version. |
| 23 | + - "CS9058" # ERR_FeatureNotAvailableInVersion11 Feature is not available in C# 11. Please use language version. |
| 24 | + - "CS8303" # ERR_LanguageVersionCannotHaveLeadingZeroes Specified language version cannot have leading zeroes |
| 25 | + - "CS8304" # ERR_CompilerAndLanguageVersion Compiler version is lower than Language version |
| 26 | + - "CS8306" # ERR_TupleInferredNamesNotAvailable Tuple element name '{0}' is inferred. Please use language version {1} or greater to access an element by its inferred name. |
| 27 | + - "CS8371" # WRN_AttributesOnBackingFieldsNotAvailable Field-targeted attributes on auto-properties are not supported in language version |
| 28 | + - "CS8401" # ERR_AltInterpolatedVerbatimStringsNotAvailable To use '@$' instead of '$@' for an interpolated verbatim string, please use language version '{0}' or greater. |
| 29 | + - "CS8511" # ERR_ConstantPatternVsOpenType An expression of type '{0}' cannot be handled by a pattern of type '{1}' |
| 30 | + - "CS8192" # ERR_BadLanguageVersion Provided language version is unsupported or invalid |
| 31 | + - "CS8627" # ERR_NullableUnconstrainedTypeParameter A nullable type parameter must be known to be a value type or non-nullable reference type |
| 32 | + - "CS8630" # ERR_NullableOptionNotAvailable Invalid '{0}' value: '{1}' for C# {2}. Please use language version '{3}' or greater |
| 33 | + - "CS8314" # ERR_PatternWrongGenericTypeInVersion An expression of type '{0}' cannot be handled by a pattern of type |
| 34 | + - "CS8652" # ERR_FeatureInPreview The feature '{0}' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version |
| 35 | + - "CS8703" # ERR_InvalidModifierForLanguageVersion The modifier '{0}' is not valid for this item in C# {1}. Please use language version '{2}' or greater. |
| 36 | + - "CS8704" # ERR_ImplicitImplementationOfNonPublicInterfaceMember Type does not implement interface member. Type cannot implicitly implement a non-public member in selected version. |
| 37 | + - "CS8706" # ERR_LanguageVersionDoesNotSupportInterfaceImplementationForMember Type cannot implement interface member '{1}' in type '{2}' because feature '{3}' is not available in |
| 38 | + - "CS8957" # ERR_NoImplicitConvTargetTypedConditional Conditional expression is not valid in language version {0} because a common type was not found between '{1}' and '{2}' |
| 39 | + - "CS8912" # ERR_InheritingFromRecordWithSealedToString Inheriting from a record with a sealed 'Object.ToString' is not supported |
| 40 | + - "CS9014" # ERR_UseDefViolationPropertyUnsupportedVersion Use of possibly unassigned property |
| 41 | + - "CS9015" # ERR_UseDefViolationFieldUnsupportedVersion Use of possibly unassigned field |
| 42 | + - "CS9016" # WRN_UseDefViolationPropertyUnsupportedVersion Use of possibly unassigned property |
| 43 | + - "CS9017" # WRN_UseDefViolationFieldUnsupportedVersion Use of possibly unassigned field |
| 44 | + - "CS8967" # ERR_NewlinesAreNotAllowedInsideANonVerbatimInterpolatedString Newlines inside a non-verbatim interpolated string are not supported in C# |
| 45 | +helpviewer_keywords: |
| 46 | + - "CS0171" |
| 47 | + - "CS0188" |
| 48 | + - "CS0843" |
| 49 | + - "CS8904" |
| 50 | + - "CS1738" |
| 51 | + - "CS8022" |
| 52 | + - "CS8023" |
| 53 | + - "CS8024" |
| 54 | + - "CS8025" |
| 55 | + - "CS8026" |
| 56 | + - "CS8059" |
| 57 | + - "CS8107" |
| 58 | + - "CS8302" |
| 59 | + - "CS8320" |
| 60 | + - "CS8370" |
| 61 | + - "CS8400" |
| 62 | + - "CS8773" |
| 63 | + - "CS8936" |
| 64 | + - "CS9058" |
| 65 | + - "CS8303" |
| 66 | + - "CS8304" |
| 67 | + - "CS8306" |
| 68 | + - "CS8371" |
| 69 | + - "CS8401" |
| 70 | + - "CS8511" |
| 71 | + - "CS8192" |
| 72 | + - "CS8627" |
| 73 | + - "CS8630" |
| 74 | + - "CS8314" |
| 75 | + - "CS8652" |
| 76 | + - "CS8703" |
| 77 | + - "CS8704" |
| 78 | + - "CS8706" |
| 79 | + - "CS8957" |
| 80 | + - "CS8912" |
| 81 | + - "CS9014" |
| 82 | + - "CS9015" |
| 83 | + - "CS9016" |
| 84 | + - "CS9017" |
| 85 | + - "CS8967" |
| 86 | +ms.date: 11/22/2022 |
| 87 | +--- |
| 88 | +# Resolve warnings related to language features and versions |
| 89 | + |
| 90 | +This article covers the following compiler warnings: |
| 91 | + |
| 92 | +<!-- The text in this list generates issues for Acrolinx, because they don't use contractions. |
| 93 | +That's be design. The text closely matches the text of the compiler error / warning for SEO purposes. |
| 94 | + --> |
| 95 | +- **CS8022, CS8023, CS8024, CS8025, CS8026, CS8059, CS8107, CS8302, CS8320, CS8370, CS8400, CS8773, CS8936, CS9058** - *Feature is not available. Use newer language version.* |
| 96 | +- **CS8192** - *Provided language version is unsupported or invalid* |
| 97 | +- **CS8303** - *Specified language version cannot have leading zeroes* |
| 98 | +- **CS8304** - *Compiler version is less than language version* |
| 99 | +- **CS1738** - *Named argument specifications must appear after all fixed arguments have been specified.* |
| 100 | +- **CS8306** - *Tuple element name is inferred.* |
| 101 | +- **CS8314** - *An expression of type cannot be handled by a pattern of type* |
| 102 | +- **CS8371** - *Field-targeted attributes on auto-properties are not supported in language version* |
| 103 | +- **CS8401** - *To use `@$` instead of `$@` for an interpolated verbatim string, use newer language version.* |
| 104 | +- **CS8511** - *An expression of type cannot be handled by a pattern of type.* |
| 105 | +- **CS8627** - *A nullable type parameter must be known to be a value type or non-nullable reference type* |
| 106 | +- **CS8630** - *Invalid nullable options. Use newer language version* |
| 107 | +- **CS8652** - *The modifier is not valid for this item.* |
| 108 | +- **CS8704** - *Type does not implement interface member. It cannot implicitly implement a non-public member.* |
| 109 | +- **CS8706** - *Type cannot implement interface member because a feature is not available in this version.* |
| 110 | +- **CS8904** - *Invalid variance: The type parameter must be valid.* |
| 111 | +- **CS8912** - *Inheriting from a record with a sealed 'Object.ToString' is not supported.* |
| 112 | +- **CS8957** - *Conditional expression is not valid in language version because a common type was not found between types.* |
| 113 | +- **CS8967** - *Newlines inside a non-verbatim interpolated string are not supported in C#* |
| 114 | +- **CS9014** - *Error: Use of possibly unassigned property. Upgrade to auto-default the property.* |
| 115 | +- **CS9015** - *Error: Use of possibly unassigned field. Upgrade to auto-default the field.* |
| 116 | +- **CS9016** - *Warning: Use of possibly unassigned property. Upgrade to auto-default the property.* |
| 117 | +- **CS9017** - *Warning: Use of possibly unassigned field. Upgrade to auto-default the field.* |
| 118 | + |
| 119 | +In addition, the following errors and warnings relate to struct initialization changes in recent versions: |
| 120 | + |
| 121 | +- [**CS0171**, **CS8881**](#breaking-changes-on-struct-initialization): *Backing field for automatically implemented property 'name' must be fully assigned before control is returned to the caller.* |
| 122 | +- [**CS0188**, **CS8885**](#breaking-changes-on-struct-initialization): *The 'this' object cannot be used before all of its fields are assigned to* |
| 123 | +- [**CS0843**, **CS8880**](#breaking-changes-on-struct-initialization): *Backing field for automatically implemented property 'name' must be fully assigned before control is returned to the caller* |
| 124 | + |
| 125 | +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. |
| 126 | + |
| 127 | +There are two possible causes and three ways to address these errors and warnings. |
| 128 | + |
| 129 | +## Update your target framework |
| 130 | + |
| 131 | +The compiler determines a default based on these rules: |
| 132 | + |
| 133 | +[!INCLUDE [langversion-table](../includes/default-langversion-table.md)] |
| 134 | + |
| 135 | +If your selected framework doesn't match the language version required, you can upgrade the target framework. |
| 136 | + |
| 137 | +## Select the matching language version |
| 138 | + |
| 139 | +You may have an older target framework selected in your project file. If you remove the `LangVersion` element from your project file, the compiler will use 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. |
| 140 | + |
| 141 | +[!INCLUDE [langversion-table](../includes/langversion-table.md)] |
| 142 | + |
| 143 | +## Avoid the updated feature |
| 144 | + |
| 145 | +If you must support older libraries or runtimes, you may need to avoid using newer features. |
| 146 | + |
| 147 | +## Breaking changes on struct initialization |
| 148 | + |
| 149 | +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. |
| 150 | + |
| 151 | +- **CS0171**, **CS8881**: *Backing field for automatically implemented property 'name' must be fully assigned before control is returned to the caller.* |
| 152 | +- **CS0188**, **CS8885**: *The 'this' object cannot be used before all of its fields are assigned to* |
| 153 | +- **CS0843**, **CS8880**: *Backing field for automatically implemented property 'name' must be fully assigned before control is returned to the caller* |
| 154 | + |
| 155 | +You can address this error by upgrading your language version to C# 11, when all fields are initialized by every `struct` constructor. If that's not a possible option, you must explicitly call the default constructor, as shown in the following example: |
| 156 | + |
| 157 | +```csharp |
| 158 | +struct S |
| 159 | +{ |
| 160 | + public int AIProp { get; set; } |
| 161 | + public S(int i){} //CS0843 |
| 162 | + // Try the following lines instead. |
| 163 | + // public S(int i) : this() |
| 164 | + // { |
| 165 | + // AIProp = i; |
| 166 | + // } |
| 167 | +} |
| 168 | + |
| 169 | +class Test |
| 170 | +{ |
| 171 | + static int Main() |
| 172 | + { |
| 173 | + return 1; |
| 174 | + } |
| 175 | +} |
| 176 | +``` |
0 commit comments