Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ f1_keywords:
- "CS9202"
- "CS9211"
- "CS9240"
- "CS9260"
helpviewer_keywords:
- "CS0171"
- "CS0188"
Expand Down Expand Up @@ -107,7 +108,8 @@ helpviewer_keywords:
- "CS9202"
- "CS9211"
- "CS9240"
ms.date: 11/02/2023
- "CS9260"
ms.date: 11/06/2024
---
# Resolve warnings related to language features and versions

Expand Down Expand Up @@ -149,6 +151,7 @@ That's be design. The text closely matches the text of the compiler error / warn
- **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.*
- **CS9240**: *Target runtime doesn't support by-ref-like generics.*
- **CS9260**: *Feature is not available in C# 13.0. Please use newer language version**

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ f1_keywords:
- "CS9183"
- "CS9184"
- "CS9189"
- "CS9259"
helpviewer_keywords:
- "CS9164"
- "CS9165"
Expand All @@ -31,7 +32,8 @@ helpviewer_keywords:
- "CS9183"
- "CS9184"
- "CS9189"
ms.date: 11/02/2023
- "CS9259"
ms.date: 11/06/2024
---
# Resolve errors and warnings with inline array declarations

Expand All @@ -54,6 +56,7 @@ That's by design. The text closely matches the text of the compiler error / warn
- [**CS9183**](#conversions-to-span): *Inline array conversion operator will not be used for conversion from expression of the declaring type.*
- [**CS9184**](#inline-array-declaration): *'Inline arrays' language feature is not supported for inline array types with element field which is either a '`ref`' field, or has type that is not valid as a type argument.*
- [**CS9189**](#element-access): *`foreach` statement on an inline array of type is not supported*
- [**CS9259**](#inline-array-declaration): *Attribute <xref:System.Runtime.CompilerServices.InlineArrayAttribute?displayProperty=fullName> cannot be applied to a record struct.*

## Inline array declaration

Expand All @@ -64,6 +67,7 @@ You declare inline arrays as a `struct` type with a single field, and an attribu
- **CS9169**: *Inline array struct must declare one and only one instance field which must not be a ref field.*
- **CS9180**: *Inline array element field cannot be declared as required, readonly, volatile, or as a fixed size buffer.*
- **CS9184**: *'Inline arrays' language feature is not supported for inline array types with element field which is either a '`ref`' field, or has type that is not valid as a type argument.*
- **CS9259**: *Attribute <xref:System.Runtime.CompilerServices.InlineArrayAttribute?displayProperty=fullName> cannot be applied to a record struct.*

To fix these arrays, ensure the following are true:

Expand All @@ -72,6 +76,7 @@ To fix these arrays, ensure the following are true:
- The enclosing `struct` has a single instance field, and that instance field is not a `ref` field.
- The single instance field is not a fixed size buffer.
- The single instance field doesn't include the `required`, `volatile`, or `readonly` modifiers.
- Remove the `record` modifier from your inline array declaration.

## Element access

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ f1_keywords:
- "CS9199"
- "CS9200"
- "CS9201"
- "CS9265"
helpviewer_keywords:
- "CS0192"
- "CS0199"
Expand Down Expand Up @@ -131,7 +132,8 @@ helpviewer_keywords:
- "CS9199"
- "CS9200"
- "CS9201"
ms.date: 11/02/2023
- "CS9265"
ms.date: 11/06/2024
---
# Errors and warnings associated with reference parameters, variables, and returns

Expand Down Expand Up @@ -207,6 +209,7 @@ The following warnings are generated when reference variables are used incorrect
- [**CS9198**](#reference-variable-restrictions): *Reference kind modifier of parameter doesn't match the corresponding parameter in target.*
- [**CS9200**](#reference-variable-restrictions): *A default value is specified for `ref readonly` parameter, but `ref readonly` should be used only for references. Consider declaring the parameter as `in`.*
- [**CS9201**](#reference-variable-restrictions): *Ref field should be ref-assigned before use.*
- [**CS9265**](#reference-variable-restrictions): *Field is never ref-assigned to, and will always have its default value (null reference)*

These errors and warnings follow these themes:

Expand Down Expand Up @@ -261,6 +264,7 @@ The following warnings indicate that a reference variable shouldn't be used, and
- **CS9198**: *Reference kind modifier of parameter doesn't match the corresponding parameter in target.*
- **CS9200**: *A default value is specified for `ref readonly` parameter, but `ref readonly` should be used only for references. Consider declaring the parameter as `in`.*
- **CS9201**: *Ref field should be ref-assigned before use.*
- **CS9265**: *Field is never ref-assigned to, and will always have its default value (null reference)*

To fix the error, remove the reference variable where it isn't allowed:

Expand All @@ -277,6 +281,7 @@ To fix the error, remove the reference variable where it isn't allowed:
- Methods attributed with <xref:System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute?displayProperty=nameWithType> can't use reference parameters.
- A Windows runtime event can't be passed as a reference variable.
- A `ref readonly` parameter can't have the <xref:System.Runtime.InteropServices.OutAttribute?displayProperty=nameWithType> applied to it in remoting API.
- Initialize a `ref` field in the constructor or as a field initializer.

## `unscoped ref` restrictions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ f1_keywords:
- "CS9245"
- "CS9246"
- "CS9247"
- "CS9267"
helpviewer_keywords:
- "CS8343"
- "CS8344"
Expand All @@ -29,7 +30,8 @@ helpviewer_keywords:
- "CS9245"
- "CS9246"
- "CS9247"
ms.date: 07/30/2024
- "CS9267"
ms.date: 11/06/2024
---
# Errors and warnings associated with `ref struct` types

Expand All @@ -46,6 +48,7 @@ ms.date: 07/30/2024
- [**CS9245**](#ref-struct-interface-implementations): *Type cannot implement interface member for `ref struct` type.*
- [**CS9246**](#ref-struct-interface-implementations): *A non-virtual instance interface member cannot be accessed on a type parameter that allows ref struct.*
- [**CS9247**](#ref-struct-interface-implementations): *foreach statement cannot operate on enumerators of type because it is a type parameter that allows ref struct and it is not known at compile time to implement `IDisposable`.*
- [**CS9267**](#ref-struct-interface-implementations): *Element type of an iterator may not be a ref struct or a type parameter allowing ref structs*

## ref safety violations

Expand Down Expand Up @@ -74,6 +77,7 @@ Violating any of these rules produces one of the listed errors. If you intended
- **CS9245**: *Type cannot implement interface member for `ref struct` type.*
- **CS9246**: *A non-virtual instance interface member cannot be accessed on a type parameter that allows ref struct.*
- **CS9247**: *foreach statement cannot operate on enumerators of type because it is a type parameter that allows ref struct and it is not known at compile time to implement `IDisposable`.*
- **CS9267**: *Element type of an iterator may not be a ref struct or a type parameter allowing ref structs*

Prior to C# 13, [`ref struct`](../builtin-types/ref-struct.md) types can't implement interfaces; the compiler generates *CS8343*. Beginning with C# 13, `ref struct` types can implement interfaces, subject to the following rules:

Expand All @@ -84,3 +88,4 @@ Beginning with C# 13, a `ref struct` can be used as a type argument for a generi

- A `ref struct` is used as a type argument, the type parameter *must* have the `allows ref struct` anti-constraint.- The `allows ref struct` anti-constraint must be last in the `where` clause for that parameter
- Uses of instances the type parameter must obey ref safety rules.
- A `ref struct` or a type argument that can be a `ref struct` type can't be used as the element type for an [iterator method](../statements/yield.md).
8 changes: 4 additions & 4 deletions docs/csharp/language-reference/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ items:
CS0171, CS0188, CS0843, CS8904, CS1738, CS8022, CS8023, CS8024, CS8025, CS8026, CS8058, CS8059, CS8107, CS8192, CS8302,
CS8303, CS8304, CS8305, CS8306, CS8314, CS8320, CS8370, CS8371, CS8400, CS8401, CS8511, CS8627, CS8630, CS8652, CS8703,
CS8704, CS8706, CS8773, CS8912, CS8919, CS8929, CS8936, CS8957, CS8967, CS9014, CS9015, CS9016, CS9017, CS9058, CS9064,
CS9103, CS9171, CS9194, CS9202, CS9204, CS9240
CS9103, CS9171, CS9194, CS9202, CS9204, CS9240, CS9260
- name: Assembly references
href: ./compiler-messages/assembly-references.md
displayName: >
Expand All @@ -463,12 +463,12 @@ items:
CS7084, CS8166, CS8167, CS8168, CS8169. CS8325, CS8326, CS8327, CS8329, CS8330, CS8331, CS8332, CS8337, CS8338, CS8351,
CS8373, CS8374, CS8388, CS8977, CS9072, CS9077, CS9078, CS9079, CS9085, CS9086, CS9087, CS9089, CS9091, CS9092, CS9093,
CS9094, CS9095, CS9096, CS9097, CS9101, CS9102, CS9104, CS9190, CS9191, CS9192, CS9193, CS9195, CS9196, CS9197, CS9198,
CS9199, CS9200, CS9201
CS9199, CS9200, CS9201, CS9265
- name: "`ref struct` types"
href: ./compiler-messages/ref-struct-errors.md
displayName: >
ref struct,
CS8343, CS8344, CS8345, CS9048, CS9050, CS9059, CS9241, CS9242, CS9243, CS9244, CS9245, CS9246, CS9247
CS8343, CS8344, CS8345, CS9048, CS9050, CS9059, CS9241, CS9242, CS9243, CS9244, CS9245, CS9246, CS9247, CS9267
- name: Iterator methods
href: ./compiler-messages/iterator-yield.md
displayName: >
Expand Down Expand Up @@ -503,7 +503,7 @@ items:
CS9188, CS9203, CS9208, CS9209, CS9210
- name: Inline arrays
href: ./compiler-messages/inline-array-errors.md
displayName: CS9164, CS9165, CS9166, CS9167, CS9168, CS9169, CS9172, CS9173, CS9180, CS9181, CS9182, CS9183, CS9184
displayName: CS9164, CS9165, CS9166, CS9167, CS9168, CS9169, CS9172, CS9173, CS9180, CS9181, CS9182, CS9183, CS9184, CS9259
- name: Lambda expressions
href: ./compiler-messages/lambda-expression-errors.md
displayName: >
Expand Down
Loading