Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -37,6 +37,11 @@ f1_keywords:
- "CS9208"
- "CS9209"
- "CS9210"
- "CS9212"
- "CS9213"
- "CS9214"
- "CS9215"
- "CS9222"
helpviewer_keywords:
- "CS0022"
- "CS0178"
Expand Down Expand Up @@ -73,6 +78,11 @@ helpviewer_keywords:
- "CS9208"
- "CS9209"
- "CS9210"
- "CS9212"
- "CS9213"
- "CS9214"
- "CS9215"
- "CS9222"
ms.date: 11/02/2023
---
# Resolve errors and warnings in array and collection declarations and initialization expressions
Expand Down Expand Up @@ -110,6 +120,11 @@ That's by design. The text closely matches the text of the compiler error / warn
- [**CS9188**](#invalid-collection-builder): *Type has a CollectionBuilderAttribute but no element type.*
- [**CS9203**](#invalid-collection-initializer): *A collection expression of this type cannot be used in this context because it may be exposed outside of the current scope.*
- [**CS9210**](#invalid-collection-initializer): *This version of <xref:System.Collections.Immutable.ImmutableArray%601?displayProperty=nameWithType>cannot be used with collection expressions.*
- [**CS9212**](#invalid-collection-initializer): *Spread operator '`..`' cannot operate on variables of type 'type' because 'type' does not contain a public instance or extension definition for 'member'*
- [**CS9213**](#invalid-collection-initializer): *Collection expression target 'type' has no element type.*
- [**CS9214**](#invalid-collection-initializer): *Collection expression type must have an applicable constructor that can be called with no arguments.*
- [**CS9215**](#invalid-collection-initializer): *Collection expression type 'type' must have an instance or extension method 'Add' that can be called with a single argument.*
- [**CS9222**](#invalid-collection-initializer): *Collection initializer results in an infinite chain of instantiations of collection 'type'.*

In addition, the following warnings are covered in this article:

Expand Down Expand Up @@ -141,6 +156,11 @@ The following errors indicate that the code generated by the compiler for a coll
- **CS9176**: *There is no target type for the collection literal.*
- **CS9203**: *A collection expression of this type cannot be used in this context because it may be exposed outside of the current scope.*
- **CS9210**: *This version of <xref:System.Collections.Immutable.ImmutableArray%601?displayProperty=nameWithType>cannot be used with collection expressions.*
- **CS9212**: *Spread operator '`..`' cannot operate on variables of type 'type' because 'type' does not contain a public instance or extension definition for 'member'*
- **CS9213**: *Collection expression target 'type' has no element type.*
- **CS9214**: *Collection expression type must have an applicable constructor that can be called with no arguments.*
- **CS9215**: *Collection expression type 'type' must have an instance or extension method 'Add' that can be called with a single argument.*
- **CS9222**: *Collection initializer results in an infinite chain of instantiations of collection 'type'.*

The compiler might also generate the following warning:

Expand All @@ -159,6 +179,11 @@ The errors all indicate that the code generated by the compiler for a collection
- Collection expressions can initialize explicitly typed variables of a collection type. If the variable isn't a collection or array type, or is implicitly typed (using `var`), a collection initializer can't be used.
- A `ref struct` type, like <xref:System.Span%601?displayProperty=nameWithType> can't be initialized with a collection expression that may violate ref safety.
- A collection expression can't correctly initialize an <xref:System.Collections.Immutable.ImmutableArray%601?displayProperty=nameWithType> using the current version. Use a different version of the runtime, or change the initialization expression.
- The spread operator (`..`) in **CS9212** requires the type to implement a suitable method (like `GetEnumerator`) to enumerate its elements. Ensure your type implements the required enumeration pattern or provides an extension method.
- **CS9213** occurs when the compiler can't determine what element type to use for the collection expression. This typically happens with custom collection types. Make sure your collection type properly exposes its element type through its type definition or implements appropriate collection interfaces.
- **CS9214** is generated when a collection expression tries to initialize a type that doesn't have a parameterless constructor. Collection expressions require a constructor that can be called with no arguments to create the instance before adding elements.
- **CS9215** happens when the collection type doesn't provide an `Add` method that accepts a single parameter of the element type. The `Add` method must be accessible (typically public) and accept exactly one argument that matches the collection's element type.
- **CS9222** indicates a circular dependency in collection initialization. This occurs when initializing a collection triggers the creation of another instance of the same collection type, which in turn requires initializing another instance, creating an infinite loop. Review your collection type's constructor and initialization logic to break the circular dependency.

The warnings indicates that the collection expression, including any [spread elements](../operators/collection-expressions.md#spread-element) might allocate memory. Creating different storage and converting might be more efficient.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
title: Resolve errors related to inline arrays
description: These compiler errors and warnings are generated when you create an inline array struct that is invalid. This article helps you diagnose and fix those issues.
f1_keywords:
- "CS9125"
- "CS9164"
- "CS9165"
- "CS9166"
Expand All @@ -18,6 +19,7 @@ f1_keywords:
- "CS9189"
- "CS9259"
helpviewer_keywords:
- "CS9125"
- "CS9164"
- "CS9165"
- "CS9166"
Expand All @@ -42,6 +44,7 @@ This article covers the following compiler errors and warnings:
<!-- The text in this list generates issues for Acrolinx, because they don't use contractions.
That's by design. The text closely matches the text of the compiler error / warning for SEO purposes.
-->
- [**CS9125**](#inline-array-declaration): *Attribute parameter 'SizeConst' must be specified.*
- [**CS9164**](#conversions-to-span): *Cannot convert expression to `Span<T>` because it is not an assignable variable*
- [**CS9165**](#conversions-to-span): *Cannot convert expression to `ReadOnlySpan<T>` because it may not be passed or returned by reference*
- [**CS9166**](#element-access): *Index is outside the bounds of the inline array*
Expand All @@ -62,6 +65,7 @@ That's by design. The text closely matches the text of the compiler error / warn

You declare inline arrays as a `struct` type with a single field, and an attribute that specifies the length of the array. The compiler generates the following errors for invalid inline array declarations:

- **CS9125**: *Attribute parameter 'SizeConst' must be specified.*
- **CS9167**: *Inline array length must be greater than 0.*
- **CS9168**: *Inline array struct must not have explicit layout.*
- **CS9169**: *Inline array struct must declare one and only one instance field which must not be a ref field.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- "CS9098" # ERR_ImplicitlyTypedDefaultParameter: Implicitly typed lambda parameter '{0}' cannot have a default value.
- "CS9099" # WRN_OptionalParamValueMismatch: The default parameter value does not match in the target delegate type.
- "CS9100" # WRN_ParamsArrayInLambdaOnly: Parameter has params modifier in lambda but not in target delegate type.
- "CS9236" # INF_TooManyBoundLambdas: Compiling requires binding the lambda expression at least {0} times.
helpviewer_keywords:
- "CS0748"
- "CS0835"
Expand All @@ -35,6 +36,7 @@
- "CS9098"
- "CS9099"
- "CS9100"
- "CS9236"
ms.date: 05/04/2023
---
# Errors and warnings when using lambda expressions and anonymous functions
Expand Down Expand Up @@ -64,6 +66,10 @@
- [**CS9099**](#lambda-expression-delegate-type): *The default parameter value does not match in the target delegate type.*
- [**CS9100**](#lambda-expression-delegate-type): *Parameter has params modifier in lambda but not in target delegate type.*

The compiler also produces the following *informational* message:

- [**CS9236**](#syntax-limitations-in-lambda-expressions): *Compiling requires binding the lambda expression at least count times. Consider declaring the lambda expression with explicit parameter types, or if the containing method call is generic, consider using explicit type arguments.*

## Syntax limitations in lambda expressions

Some C# syntax is prohibited in lambda expressions and anonymous methods. Using invalid constructs in a lambda expression causes the following errors:
Expand All @@ -90,7 +96,12 @@

- **CS8971**: *InterpolatedStringHandlerArgument has no effect when applied to lambda parameters and will be ignored at the call site.*

Certain expressions cause the compiler to emit the following informational warning:

- **CS9236**: *Compiling requires binding the lambda expression at least count times. Consider declaring the lambda expression with explicit parameter types, or if the containing method call is generic, consider using explicit type arguments.*

The complexity of the lambda expressions and how they invoke other lambda expressions is negatively impacting compiler performance. The reason is that the compiler must infer parameter and argument types through the lambda expressions and the potential types takes time.
## Lambda expression parameters and returns

Check failure on line 104 in docs/csharp/language-reference/compiler-messages/lambda-expression-errors.md

View workflow job for this annotation

GitHub Actions / lint

Headings should be surrounded by blank lines

docs/csharp/language-reference/compiler-messages/lambda-expression-errors.md:104 MD022/blanks-around-headings Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Above] [Context: "## Lambda expression parameters and returns"] https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md022.md

These errors indicate a problem with a parameter declaration:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ f1_keywords:
- "CS9199"
- "CS9200"
- "CS9201"
- "CS9205"
- "CS9265"
helpviewer_keywords:
- "CS0192"
Expand Down Expand Up @@ -132,6 +133,7 @@ helpviewer_keywords:
- "CS9199"
- "CS9200"
- "CS9201"
- "CS9205"
- "CS9265"
ms.date: 11/06/2024
---
Expand Down Expand Up @@ -209,6 +211,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.*
- [**CS9205**](#incorrect-syntax): *Expected interpolated string*
- [**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 All @@ -228,12 +231,14 @@ These errors indicate that you're using incorrect syntax regarding reference var
- **CS8373**: *The left-hand side of a `ref` assignment must be a ref variable.*
- **CS8388**: *An `out` variable cannot be declared as a ref local.*
- **CS9190**: *`readonly` modifier must be specified after `ref`.*
- **CS9205**: *Expected interpolated string*

You can correct the error with one of these changes:

- The left operand of an `= ref` operator must be a reference variable. For more information on the correct syntax, see [reference variables](../statements/declarations.md#reference-variables).
- The parameter modifier `ref readonly` must be in that order. `readonly ref` is not a legal parameter modifier. Switch the order of the words.
- A local variable can't be declared as `out`. To declare a local reference variable, use `ref`.
- An `out` argument can't be an interpolated string.

## Reference variable restrictions

Expand Down
Loading
Loading