You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -50,266 +50,87 @@ There are a few *errors* related to the `lock` statement and thread synchronizat
50
50
<!-- The text in this list generates issues for Acrolinx, because they don't use contractions.
51
51
That's by design. The text closely matches the text of the compiler error / warning for SEO purposes.
52
52
-->
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.*
69
69
70
70
In addition, the compiler might produce the following *warning* related to the `params` modifier on method parameters:
71
71
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
-
publicclassMyClass
88
-
{
89
-
publicstaticvoidTestParams(paramsinta) // CS0225
90
-
// try the following line instead
91
-
// public static void TestParams(params int[] a)
92
-
{
93
-
}
94
-
95
-
publicstaticvoidMain()
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).
// To resolve the error, use the following line instead:
116
-
// public void TestMethod(int i, params int[] p) {}
117
-
118
-
staticvoidMain()
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
-
interfaceI
135
-
{
136
-
voidF1(paramsint[] a);
137
-
voidF2(int[] a);
138
-
}
139
-
140
-
classC : I
141
-
{
142
-
void I.F1(paramsint[] a) {}
143
-
void I.F2(paramsint[] a) {} // CS0466
144
-
void I.F2(int[] a) {} // OK
145
-
146
-
publicstaticvoidMain()
147
-
{
148
-
Ii= (I) newC();
149
-
150
-
i.F1(newint[] {1, 2} );
151
-
i.F2(newint[] {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.*
160
75
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
162
77
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:
// public static void UseParams(params int[] list)
174
-
{
175
-
for ( inti=0 ; i<list.Length ; i++ )
176
-
Console.WriteLine(list[i]);
177
-
Console.WriteLine();
178
-
}
179
-
180
-
publicstaticvoidMain()
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
-
usingSystem;
199
-
200
-
publicpartialclassC
201
-
{
202
-
partialvoidPart(inti, paramschar[] array);
203
-
partialvoidPart(inti, char[] array) // CS0758
204
-
{
205
-
}
206
-
207
-
publicstaticintMain()
208
-
{
209
-
return1;
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.
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
-
publicclassMyClass
244
-
{
245
-
publicstaticvoidTest(paramsrefint[] a) // CS1611, remove ref
246
-
{
247
-
}
248
-
249
-
publicstaticvoidMain()
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.*
255
85
256
-
## Compiler Error CS1670
86
+
The compiler enforces the following rules on your use of the `params` modifier on a method parameter:
257
87
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.
259
93
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.
261
95
262
-
- Parameter lists of anonymous methods
263
-
- Overloaded operators
96
+
## Parameter and argument type rules
264
97
265
-
The following sample generates CS1670:
98
+
The following errors indicate that the type of the parameter used with `params` is invalid:
-**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.*
275
105
276
-
staticvoidMain()
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.
281
107
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.*
283
111
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.
285
113
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).
287
115
288
-
```csharp
289
-
// CS1751.cs
290
-
voidMethod(paramsobject[] values=null)
291
-
{
292
-
}
293
-
```
116
+
## Other params errors
294
117
295
-
To fix the error:
118
+
The following errors indicate other issues with using the `params` modifier:
296
119
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.*
301
126
302
-
voidMethod(paramsobject[] 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.
306
128
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>.
0 commit comments