Skip to content

Commit 14a9259

Browse files
committed
Edit pass to combine different concepts
Combine errors on different concepts related to the `params` modifier.
1 parent 1c2f321 commit 14a9259

File tree

1 file changed

+57
-236
lines changed

1 file changed

+57
-236
lines changed

docs/csharp/language-reference/compiler-messages/params-arrays.md

Lines changed: 57 additions & 236 deletions
Original file line numberDiff line numberDiff line change
@@ -50,266 +50,87 @@ There are a few *errors* related to the `lock` statement and thread synchronizat
5050
<!-- The text in this list generates issues for Acrolinx, because they don't use contractions.
5151
That's by design. The text closely matches the text of the compiler error / warning for SEO purposes.
5252
-->
53-
- **CS0225**: *The params parameter must be a single-dimensional array or have a valid collection type*
54-
- **CS0231**: *A params parameter must be the last parameter in a formal parameter list.*
55-
- **CS0466**: *'method1' should not have a params parameter since 'method2' does not*
56-
- **CS0674**: *Do not use `System.ParamArrayAttribute` or `System.ParamArrayAttribute`/`System.Runtime.CompilerServices.ParamCollectionAttribute`. Use the `params` keyword instead.*
57-
- **CS0758**: *Both partial method declarations must use a `params` parameter or neither may use a `params` parameter*
58-
- **CS1104**: *A parameter array cannot be used with `this` modifier on an extension method.*
59-
- **CS1611**: *The params parameter cannot be declared as in `ref` or `out`*
60-
- **CS1670**: *`params` is not valid in this context*
61-
- **CS1751**: *Cannot specify a default value for a parameter array.*
62-
- **CS9218**: *The type arguments for method cannot be inferred from the usage because an argument with dynamic type is used and the method has a non-array params collection parameter. Try specifying the type arguments explicitly.*
63-
- **CS9219**: *Ambiguity between expanded and normal forms of non-array params collection parameter of, the only corresponding argument has the type 'dynamic'. Consider casting the dynamic argument.*
64-
- **CS9223**: *Creation of params collection results in an infinite chain of invocation of constructor.*
65-
- **CS9224**: *Method cannot be less visible than the member with params collection.*
66-
- **CS9225**: *Constructor leaves required member uninitialized.*
67-
- **CS9227**: *Type does not contain a definition for a suitable instance `Add` method.*
68-
- **CS9228**: *Non-array params collection type must have an applicable constructor that can be called with no arguments.*
53+
- [**CS0225**](#parameter-and-argument-type-rules): *The params parameter must be a single-dimensional array or have a valid collection type*
54+
- [**CS0231**](#method-declaration-rules): *A params parameter must be the last parameter in a formal parameter list.*
55+
- [**CS0466**](#other-params-errors): *'method1' should not have a params parameter since 'method2' does not*
56+
- [**CS0674**](#other-params-errors): *Do not use `System.ParamArrayAttribute` or `System.ParamArrayAttribute`/`System.Runtime.CompilerServices.ParamCollectionAttribute`. Use the `params` keyword instead.*
57+
- [**CS0758**](#other-params-errors): *Both partial method declarations must use a `params` parameter or neither may use a `params` parameter*
58+
- [**CS1104**](#method-declaration-rules): *A parameter array cannot be used with `this` modifier on an extension method.*
59+
- [**CS1611**](#method-declaration-rules): *The params parameter cannot be declared as in `ref` or `out`*
60+
- [**CS1670**](#method-declaration-rules): *`params` is not valid in this context*
61+
- [**CS1751**](#method-declaration-rules): *Cannot specify a default value for a parameter array.*
62+
- [**CS9218**](#parameter-and-argument-type-rules): *The type arguments for method cannot be inferred from the usage because an argument with dynamic type is used and the method has a non-array params collection parameter. Try specifying the type arguments explicitly.*
63+
- [**CS9219**](#parameter-and-argument-type-rules): *Ambiguity between expanded and normal forms of non-array params collection parameter of, the only corresponding argument has the type `dynamic`. Consider casting the dynamic argument.*
64+
- [**CS9223**](#other-params-errors): *Creation of params collection results in an infinite chain of invocation of constructor.*
65+
- [**CS9224**](#other-params-errors): *Method cannot be less visible than the member with params collection.*
66+
- [**CS9225**](#other-params-errors): *Constructor leaves required member uninitialized.*
67+
- [**CS9227**](#parameter-and-argument-type-rules): *Type does not contain a definition for a suitable instance `Add` method.*
68+
- [**CS9228**](#parameter-and-argument-type-rules): *Non-array params collection type must have an applicable constructor that can be called with no arguments.*
6969

7070
In addition, the compiler might produce the following *warning* related to the `params` modifier on method parameters:
7171

72-
- **CS9220**: *One or more overloads of method having non-array params collection parameter might be applicable only in expanded form which is not supported during dynamic dispatch.*
73-
- **CS9221**: *One or more indexer overloads having non-array params collection parameter might be applicable only in expanded form which is not supported during dynamic dispatch.*
74-
- **CS9222**: *One or more constructor overloads having non-array params collection parameter might be applicable only in expanded form which is not supported during dynamic dispatch.*
75-
76-
## Compiler Error CS0225
77-
78-
The params parameter must be a single-dimensional array
79-
The params parameter must have a valid collection type
80-
81-
When using the [params](../keywords/method-parameters.md#params-modifier) keyword, you must specify a single-dimensional array or collection of the data type. For more information, see [Methods](../../programming-guide/classes-and-structs/methods.md).
82-
83-
The following sample generates CS0225:
84-
85-
```csharp
86-
// CS0225.cs
87-
public class MyClass
88-
{
89-
public static void TestParams(params int a) // CS0225
90-
// try the following line instead
91-
// public static void TestParams(params int[] a)
92-
{
93-
}
94-
95-
public static void Main()
96-
{
97-
TestParams(1);
98-
}
99-
}
100-
```
101-
102-
## Compiler Error CS0231
103-
104-
A params parameter must be the last parameter in a formal parameter list.
105-
106-
The [params](../keywords/method-parameters.md#params-modifier) parameter supports a variable number of arguments and must be after all other parameters. For more information, see [Methods](../../programming-guide/classes-and-structs/methods.md).
107-
108-
The following sample generates CS0231:
109-
110-
```csharp
111-
// CS0231.cs
112-
class Test
113-
{
114-
public void TestMethod(params int[] p, int i) {} // CS0231
115-
// To resolve the error, use the following line instead:
116-
// public void TestMethod(int i, params int[] p) {}
117-
118-
static void Main()
119-
{
120-
}
121-
}
122-
```
123-
124-
## Compiler Error CS0466
125-
126-
'method1' should not have a params parameter since 'method2' does not
127-
128-
You cannot use `params` parameter on a class member if the implemented interface doesn't use it.
129-
130-
The following sample generates CS0466.
131-
132-
```csharp
133-
// CS0466.cs
134-
interface I
135-
{
136-
void F1(params int[] a);
137-
void F2(int[] a);
138-
}
139-
140-
class C : I
141-
{
142-
void I.F1(params int[] a) {}
143-
void I.F2(params int[] a) {} // CS0466
144-
void I.F2(int[] a) {} // OK
145-
146-
public static void Main()
147-
{
148-
I i = (I) new C();
149-
150-
i.F1(new int[] {1, 2} );
151-
i.F2(new int[] {1, 2} );
152-
}
153-
}
154-
```
155-
156-
## Compiler Error CS0674
157-
158-
Do not use 'System.ParamArrayAttribute'. Use the 'params' keyword instead.
159-
Do not use 'System.ParamArrayAttribute'/'System.Runtime.CompilerServices.ParamCollectionAttribute'. Use the 'params' keyword instead.
72+
- [**CS9220**](#parameter-and-argument-type-rules): *One or more overloads of method having non-array params collection parameter might be applicable only in expanded form which is not supported during dynamic dispatch.*
73+
- [**CS9221**](#parameter-and-argument-type-rules): *One or more indexer overloads having non-array params collection parameter might be applicable only in expanded form which is not supported during dynamic dispatch.*
74+
- [**CS9222**](#parameter-and-argument-type-rules): *One or more constructor overloads having non-array params collection parameter might be applicable only in expanded form which is not supported during dynamic dispatch.*
16075

161-
The C# compiler does not allow for the use of <xref:System.ParamArrayAttribute?displayProperty=nameWithType>; use [params](../keywords/method-parameters.md#params-modifier) instead.
76+
## Method declaration rules
16277

163-
The following sample generates CS0674:
78+
The following errors indicate using a `params` modifier on a parameter when the `params` modifier isn't allowed in that context:
16479

165-
```csharp
166-
// CS0674.cs
167-
using System;
168-
public class MyClass
169-
{
170-
171-
public static void UseParams([ParamArray] int[] list) // CS0674
172-
// try the following line instead
173-
// public static void UseParams(params int[] list)
174-
{
175-
for ( int i = 0 ; i < list.Length ; i++ )
176-
Console.WriteLine(list[i]);
177-
Console.WriteLine();
178-
}
179-
180-
public static void Main()
181-
{
182-
UseParams(1, 2, 3);
183-
}
184-
}
185-
```
186-
187-
## Compiler Error CS0758
188-
189-
Both partial method declarations must use a params parameter or neither may use a params parameter
190-
191-
If one part of a partial method specifies a `params` parameter, the other part must specify one also.
192-
193-
Either add the `params` modifier in one part of the method, or remove it in the other.
194-
195-
The following code generates CS0758:
196-
197-
```csharp
198-
using System;
199-
200-
public partial class C
201-
{
202-
partial void Part(int i, params char[] array);
203-
partial void Part(int i, char[] array) // CS0758
204-
{
205-
}
206-
207-
public static int Main()
208-
{
209-
return 1;
210-
}
211-
}
212-
```
213-
214-
## Compiler Error CS1104
215-
216-
A parameter array cannot be used with 'this' modifier on an extension method.
217-
218-
The first parameter of an extension method cannot be a params array.
219-
220-
Remember that the first parameter of an extension method definition specifies which type the method will "extend". It is not an input parameter. Therefore, it makes no sense to have a params array in this location. If you do have to pass in a params array, make it the second parameter.
221-
222-
The following example generates CS1104:
223-
224-
```csharp
225-
// cs1104.cs
226-
// Compile with: /target:library
227-
public static class Extensions
228-
{
229-
public static void Test<T>(this params T[] tArr) {} // CS1104
230-
}
231-
```
232-
233-
## Compiler Error CS1611
234-
235-
The params parameter cannot be declared as in ref or out
236-
237-
The keywords [in](../keywords/method-parameters.md#in-parameter-modifier), [ref](../keywords/ref.md) or [out](../keywords/method-parameters.md#out-parameter-modifier) cannot be used with the [params](../keywords/method-parameters.md#params-modifier) keyword.
238-
239-
The following sample generates CS1611:
240-
241-
```csharp
242-
// CS1611.cs
243-
public class MyClass
244-
{
245-
public static void Test(params ref int[] a) // CS1611, remove ref
246-
{
247-
}
248-
249-
public static void Main()
250-
{
251-
Test(1);
252-
}
253-
}
254-
```
80+
- **CS0231**: *A params parameter must be the last parameter in a formal parameter list.*
81+
- **CS1104**: *A parameter array cannot be used with `this` modifier on an extension method.*
82+
- **CS1611**: *The params parameter cannot be declared as in `ref` or `out`*
83+
- **CS1670**: *`params` is not valid in this context*
84+
- **CS1751**: *Cannot specify a default value for a parameter array.*
25585

256-
## Compiler Error CS1670
86+
The compiler enforces the following rules on your use of the `params` modifier on a method parameter:
25787

258-
params is not valid in this context
88+
- The `params` modifier is allowed only on the last parameter in a formal parameter list. This includes any parameters with a default value.
89+
- You can't include a default argument for the parameter when the `params` modifier is used.
90+
- The `params` modifier can't be applied to reference parameter. A reference parameter is one with the `in`, `ref readonly`, `ref` or `out` modifier.
91+
- The `params` modifier can't be combined with the `this` modifier on an extension method.
92+
- The `params` modifier can't be used on an overloaded operator.
25993

260-
A number of C# features are incompatible with variable argument lists, and do not allow the `params` keyword, including the following:
94+
In versions before C# 12, the `params` modifier can't be used on the parameter of an anonymous method or lambda expression.
26195

262-
- Parameter lists of anonymous methods
263-
- Overloaded operators
96+
## Parameter and argument type rules
26497

265-
The following sample generates CS1670:
98+
The following errors indicate that the type of the parameter used with `params` is invalid:
26699

267-
```csharp
268-
// CS1670.cs
269-
public class C
270-
{
271-
public bool operator +(params int[] paramsList) // CS1670
272-
{
273-
return false;
274-
}
100+
- **CS9218**: *The type arguments for method cannot be inferred from the usage because an argument with dynamic type is used and the method has a non-array params collection parameter. Try specifying the type arguments explicitly.*
101+
- **CS9219**: *Ambiguity between expanded and normal forms of non-array params collection parameter of, the only corresponding argument has the type 'dynamic'. Consider casting the dynamic argument.*
102+
- **CS0225**: *The params parameter must be a single-dimensional array or have a valid collection type*
103+
- **CS9227**: *Type does not contain a definition for a suitable instance `Add` method.*
104+
- **CS9228**: *Non-array params collection type must have an applicable constructor that can be called with no arguments.*
275105

276-
static void Main()
277-
{
278-
}
279-
}
280-
```
106+
The following warnings indicate that the one of the possible overloads might involve dynamic dispatch. The dynamic nature of the argument doesn't participate in overload resolution.
281107

282-
## Compiler Error CS1751
108+
- **CS9220**: *One or more overloads of method having non-array params collection parameter might be applicable only in expanded form which is not supported during dynamic dispatch.*
109+
- **CS9221**: *One or more indexer overloads having non-array params collection parameter might be applicable only in expanded form which is not supported during dynamic dispatch.*
110+
- **CS9222**: *One or more constructor overloads having non-array params collection parameter might be applicable only in expanded form which is not supported during dynamic dispatch.*
283111

284-
Cannot specify a default value for a parameter array.
112+
In versions before C# 13, the `params` modifier is allowed on single-dimensional arrays only. No other types were valid.
285113

286-
The following sample generates CS1751:
114+
Starting with C# 13 any valid collection type can be used. However, some restrictions remain. The collection type must follow the same rules as the target of a [collection expression](../operators/collection-expressions.md#conversions).
287115

288-
```csharp
289-
// CS1751.cs
290-
void Method(params object[] values = null)
291-
{
292-
}
293-
```
116+
## Other params errors
294117

295-
To fix the error:
118+
The following errors indicate other issues with using the `params` modifier:
296119

297-
```csharp
298-
// Explicitly passing null
299-
object[] values = null;
300-
Method(values);
120+
- **CS0466**: *'method1' should not have a params parameter since 'method2' does not*
121+
- **CS0674**: *Do not use `System.ParamArrayAttribute` or `System.Runtime.CompilerServices.ParamCollectionAttribute`. Use the `params` keyword instead.*
122+
- **CS0758**: *Both partial method declarations must use a `params` parameter or neither may use a `params` parameter*
123+
- **CS9223**: *Creation of params collection results in an infinite chain of invocation of constructor.*
124+
- **CS9224**: *Method cannot be less visible than the member with params collection.*
125+
- **CS9225**: *Constructor leaves required member uninitialized.*
301126

302-
void Method(params object[] values)
303-
{
304-
if (values == null)
305-
{
127+
A method that implements an interface must include the `params` modifier if and only if the interface member has the `params` modifier. Similarly, either both declarations of a `partial` method must include the `params` modifier, or none can include the `params` modifier.
306128

307-
}
308-
}
309-
```
129+
You must use the `params` modifier. You can't apply the equivalent attributes, either <xref:System.ParamArrayAttribute?displayProperty=nameWithType> or <xref:System.Runtime.CompilerServices.ParamCollectionAttribute?displayProperty=nameWithType>.
310130

311131
## See also
312132

313133
- [Extension Methods](../../programming-guide/classes-and-structs/extension-methods.md)
314134
- [Partial Classes and Methods](../../programming-guide/classes-and-structs/partial-classes-and-methods.md)
135+
- [Collection expressions](../operators/collection-expressions.md)
315136
- [params](../keywords/method-parameters.md#params-modifier)

0 commit comments

Comments
 (0)