Skip to content
Open
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
8 changes: 8 additions & 0 deletions .openpublishing.redirection.csharp.json
Original file line number Diff line number Diff line change
Expand Up @@ -1856,6 +1856,10 @@
"source_path_from_root": "/docs/csharp/misc/cs0081.md",
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors"
},
{
"source_path_from_root": "/docs/csharp/misc/cs0101.md",
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors"
},
{
"source_path_from_root": "/docs/csharp/misc/cs0104.md",
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors#cs0104"
Expand Down Expand Up @@ -2916,6 +2920,10 @@
"source_path_from_root": "/docs/csharp/misc/cs1526.md",
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/new-object-creation-errors"
},
{
"source_path_from_root": "/docs/csharp/misc/cs1527.md",
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors"
},
{
"source_path_from_root": "/docs/csharp/misc/cs1529.md",
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ helpviewer_keywords:
- "CS9357"
- "CS9358"
- "CS9359"
ms.date: 02/04/2026
ms.date: 09/18/2026
ai-usage: ai-assisted
---
# Resolve errors and warnings in array and collection declarations and initialization expressions

This article covers the following compiler errors:

<!-- 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.
That's by design. The text closely matches the text of the compiler error or warning for SEO purposes.
-->
- [**CS0022**](#invalid-array-element-access): *Wrong number of indices inside [], expected 'number'*
- [**CS0178**](#invalid-array-rank): *Invalid rank specifier: expected '`,`' or '`]`'*
Expand Down Expand Up @@ -141,14 +141,14 @@ That's by design. The text closely matches the text of the compiler error / warn
- [**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'.*
- [**CS9332**](#invalid-collection-initializer): *Cannot use '..' spread operator in the filter expression of a catch clause.*
- [**CS9354**](#invalid-collection-initializer): *'with(...)' element must be the first element*
- [**CS9355**](#invalid-collection-initializer): *'with(...)' elements are not supported for type 'type'*
- [**CS9356**](#invalid-collection-initializer): *'with(...)' element arguments cannot be dynamic*
- [**CS9357**](#invalid-collection-initializer): *'with(...)' element for a read-only interface must be empty if present*
- [**CS9358**](#invalid-collection-initializer): *Element type of this collection may not be a ref struct or a type parameter allowing ref structs*
- [**CS9359**](#invalid-collection-initializer): *No overload for method 'method' takes 'number' 'with(...)' element arguments*
- [**CS9354**](#invalid-collection-expression-arguments): *'with(...)' element must be the first element*
- [**CS9355**](#invalid-collection-expression-arguments): *'with(...)' elements are not supported for type 'type'*
- [**CS9356**](#invalid-collection-expression-arguments): *'with(...)' element arguments cannot be dynamic*
- [**CS9357**](#invalid-collection-expression-arguments): *'with(...)' element for a read-only interface must be empty if present*
- [**CS9358**](#invalid-collection-expression-element-type): *Element type of this collection may not be a ref struct or a type parameter allowing ref structs*
- [**CS9359**](#invalid-collection-expression-arguments): *No overload for method 'method' takes number 'with(...)' element arguments*

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

- [**CS1062**](#invalid-collection-initializer): *The best overloaded Add method for the collection initializer element is obsolete.*
- [**CS1064**](#invalid-collection-initializer): *The best overloaded Add method for the collection initializer element is obsolete.*
Expand Down Expand Up @@ -189,12 +189,6 @@ To access array elements correctly, follow these indexing rules. For more inform
- **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'.*
- **CS9332**: *Cannot use '..' spread operator in the filter expression of a catch clause.*
- **CS9354**: *'with(...)' element must be the first element*
- **CS9355**: *'with(...)' elements are not supported for type 'type'*
- **CS9356**: *'with(...)' element arguments cannot be dynamic*
- **CS9357**: *'with(...)' element for a read-only interface must be empty if present*
- **CS9358**: *Element type of this collection may not be a ref struct or a type parameter allowing ref structs*
- **CS9359**: *No overload for method 'method' takes 'number' 'with(...)' element arguments*

The compiler might also generate the following warnings:

Expand All @@ -219,12 +213,28 @@ To create valid collection initializers, follow these rules. For more informatio
- Implement enumeration patterns (like `GetEnumerator`) for spread operator support (**CS9212**).
- Avoid circular dependencies in collection initialization (**CS9222**).
- Don't use the spread operator in catch clause filter expressions (**CS9332**).
- Place the `with(...)` element first in collection expressions (**CS9354**).
- Use `with(...)` elements only with types that support collection expression arguments (**CS9355**).
- Don't use dynamic arguments in `with(...)` elements (**CS9356**).
- Use empty `with()` for read-only interface types (**CS9357**).
- Don't use ref struct types as element types in collections that don't support them (**CS9358**).
- Match the number of `with(...)` arguments to available constructor overloads (**CS9359**).

## Invalid collection expression arguments

- **CS9354**: *'with(...)' element must be the first element*
- **CS9355**: *'with(...)' elements are not supported for type 'type'*
- **CS9356**: *'with(...)' element arguments cannot be dynamic*
- **CS9357**: *'with(...)' element for a read-only interface must be empty if present*
- **CS9359**: *No overload for method 'method' takes number 'with(...)' element arguments*

For the supported targets and syntax, see [Collection expression arguments](../operators/collection-expressions.md#collection-expression-arguments).

- **CS9354**: The `with(...)` element follows another collection element, or the expression contains more than one `with(...)` element. Move the single `with(...)` element to the first position.
- **CS9355**: The target type doesn't support collection expression arguments, as with arrays and span types. Remove the `with(...)` element, or change the target to a type that supports collection expression arguments.
- **CS9356**: An argument in the `with(...)` element has the compile-time type `dynamic`. Cast or convert each dynamic argument to the intended non-dynamic type.
- **CS9357**: A read-only interface target supports only an empty `with()` element. Remove the arguments, remove the `with()` element, or use a target type that accepts those arguments.
- **CS9359**: No accessible constructor, collection builder method, or supported interface signature accepts the supplied number of `with(...)` arguments. Match the arguments to an applicable constructor or interface signature. For a collection builder, match them to parameters before the final `ReadOnlySpan<T>` parameter, or add a matching overload.

## Invalid collection expression element type

- **CS9358**: *Element type of this collection may not be a ref struct or a type parameter allowing ref structs*

The collection target doesn't support a `ref struct` element type or a type parameter that allows ref-like types. Use a non-ref-like element type. For generic code, use a type parameter that doesn't allow ref-like types.

## Invalid array rank

Expand Down Expand Up @@ -276,7 +286,7 @@ The following example shows both mechanisms:
To use arrays with correct element types, follow these type restrictions. For more information, see [Implicitly typed local variables](../statements/declarations.md#implicitly-typed-local-variables) and [best common type](~/_csharpstandard/standard/expressions.md#126317-finding-the-best-common-type-of-a-set-of-expressions).

- Don't use restricted types like <xref:System.TypedReference?displayProperty=fullName> and <xref:System.ArgIterator?displayProperty=fullName> as array element types (**CS0611**).
- Don't use `static` classes as array element types because instances can't be created (**CS0719**).
- Don't use `static` classes as array element types because you can't create instances (**CS0719**).
- Initialize implicitly typed arrays with a `new` expression (**CS0820**).
- Ensure all elements in an implicitly typed array initializer have a best common type (**CS0826**).

Expand All @@ -293,7 +303,7 @@ To ensure a best common type, use any of these techniques:
## Invalid array initializer

- **CS0623**: *Array initializers can only be used in a variable or field initializer. Try using a new expression instead.*
- **CS0846**: *A nested array initializer is expected*
- **CS0846**: *A nested array initializer is expected.*
- **CS1925**: *Cannot initialize object of type 'type' with a collection initializer.*

These errors indicate invalid array initializer syntax. For more information, see [Arrays](../builtin-types/arrays.md).
Expand Down Expand Up @@ -352,7 +362,7 @@ To create collection expressions with collection builder attributes correctly, f
- Avoid using collection expressions in expression trees (**CS9175**).
- Use the correct language version for collection expressions and related features (**CS9196**, **CS9197**, **CS9198**, **CS9199**, **CS9202**).

## Common language specification warnings
## Common Language Specification warnings

- **CS3007**: *Overloaded method 'method' differing only by unnamed array types is not CLS-compliant*
- **CS3016**: *Arrays as attribute arguments is not CLS-compliant*
Expand All @@ -362,6 +372,6 @@ To write CLS-compliant code with arrays, follow these guidelines. For more infor
- Don't create overloaded methods that differ only in array element types (**CS3007**).
- Don't use arrays as attribute arguments (**CS3016**).

CS3007 occurs if you have an overloaded method that takes a jagged array and the only difference between the method signatures is the element type of the array. To avoid this error, consider using a rectangular array rather than a jagged array or, if CLS Compliance isn't needed, remove the <xref:System.CLSCompliantAttribute> attribute. For more information on CLS Compliance, see [Language independence and language-independent components](../../../standard/language-independence.md).
CS3007 occurs if you have an overloaded method that takes a jagged array and the only difference between the method signatures is the element type of the array. To avoid this error, consider using a rectangular array rather than a jagged array or, if CLS compliance isn't needed, remove the <xref:System.CLSCompliantAttribute> attribute. For more information about CLS compliance, see [Language independence and language-independent components](../../../standard/language-independence.md).

CS3016 indicates that not compliant with the Common Language Specification (CLS) to pass an array to an attribute. For more information on CLS compliance, see [Language independence and language-independent components](../../../standard/language-independence.md).
CS3016 indicates that passing an array to an attribute isn't compliant with the Common Language Specification (CLS). For more information about CLS compliance, see [Language independence and language-independent components](../../../standard/language-independence.md).
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ f1_keywords:
- "CS1018" # ERR_ThisOrBaseExpected
- "CS8054" # ERR_EnumsCantContainDefaultConstructor
- "CS8091" # ERR_ExternHasConstructorInitializer
- "CS8760" # ERR_ExternEventInitializer
- "CS8358" # ERR_AttributeCtorInParameter
- "CS8760" # ERR_ExternEventInitializer
- "CS8813" # ERR_ModuleInitializerMethodMustBeOrdinary
- "CS8814" # ERR_ModuleInitializerMethodMustBeAccessibleOutsideTopLevelType
- "CS8815" # ERR_ModuleInitializerMethodMustBeStaticParameterlessVoid
Expand Down Expand Up @@ -62,6 +62,7 @@ f1_keywords:
- "CS9124"
- "CS9136"
- "CS9179"
- "CS9343"
helpviewer_keywords:
- "CS0132"
- "CS0514"
Expand Down Expand Up @@ -122,14 +123,15 @@ helpviewer_keywords:
- "CS9124"
- "CS9136"
- "CS9179"
ms.date: 05/19/2026
- "CS9343"
ms.date: 09/17/2026
---
# Resolve errors and warnings for constructor declarations and module initializers

This article covers the following compiler errors:

<!-- 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.
That's by design. The text closely matches the text of the compiler error or warning for SEO purposes.
-->
- [**CS0132**](#static-constructors): *'constructor': a static constructor must be parameterless.*
- [**CS0514**](#static-constructors): *static constructor cannot have an explicit 'this' or 'base' constructor call.*
Expand All @@ -145,8 +147,8 @@ That's by design. The text closely matches the text of the compiler error / warn
- [**CS1018**](#constructor-calls-with-base-and-this): *Keyword 'this' or 'base' expected.*
- [**CS8054**](#constructor-declaration): *Enums cannot contain explicit parameterless constructors.*
- [**CS8091**](#constructor-declaration): *cannot be extern and have a constructor initializer.*
- [**CS8760**](#constructor-declaration): *'event': extern event cannot have initializer.*
- [**CS8358**](#constructor-declaration): *Cannot use attribute constructor because it has 'in' or 'ref readonly' parameters.*
- [**CS8760**](#constructor-declaration): *'event': extern event cannot have initializer.*
- [**CS8813**](#module-initializer-declarations): *A module initializer must be an ordinary member method*
- [**CS8814**](#module-initializer-declarations): *Module initializer method 'method' must be accessible at the module level*
- [**CS8815**](#module-initializer-declarations): *Module initializer method 'method' must be static, and non-virtual, must have no parameters, and must return 'void'*
Expand Down Expand Up @@ -180,8 +182,9 @@ That's by design. The text closely matches the text of the compiler error / warn
- [**CS9121**](#primary-constructor-declaration): *Struct primary constructor parameter of type causes a cycle in the struct layout.*
- [**CS9122**](#primary-constructor-declaration): *Unexpected parameter list.*
- [**CS9136**](#primary-constructor-declaration): *Cannot use primary constructor parameter of type inside an instance member.*
- [**CS9343**](#primary-constructor-declaration): *Cannot pass arguments to the base type without a parameter list on the type declaration.*

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

- [**CS0824**](#constructor-declaration): *Constructor 'name' is marked external.*
- [**CS9107**](#primary-constructor-declaration): *Parameter is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.*
Expand Down Expand Up @@ -325,13 +328,15 @@ For more information, see <xref:System.Runtime.CompilerServices.ModuleInitialize
- **CS8861**: *Unexpected argument list.*
- **CS8862**: *A constructor declared in a type with parameter list must have 'this' constructor initializer.*
- **CS9122**: *Unexpected parameter list.*
- **CS9343**: *Cannot pass arguments to the base type without a parameter list on the type declaration.*

When a type has a primary constructor, all other explicitly declared constructors must chain to it by using `: this(...)`.

To fix these errors, try the following suggestions:

- Add a `: this(...)` initializer that passes appropriate arguments to the primary constructor, because all explicitly declared constructors must chain to the primary constructor (**CS8862**).
- Remove a parameter list from the base type reference when the base type doesn't have a primary constructor, because the syntax `class Derived : Base(args)` is only valid when `Base` has a primary constructor (**CS8861**).
- Remove an argument list from an implemented interface or from the base-type list of a struct or interface. Only a class declaration with a parameter list can pass arguments to a base class in the base-type list (**CS8861**).
- Add a parameter list to the derived type declaration before passing arguments to the base class, for example `class Derived(int value) : Base(value) { }` (**CS9343**). The base class must have a matching constructor, but it doesn't need a primary constructor.
- Remove a primary constructor parameter list from an `interface` declaration, because interfaces can't have primary constructors (**CS9122**).

### Parameter usage in base constructor calls
Expand Down
Loading
Loading