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
"_csharplang/proposals/field-keyword.md": "This proposal introduces a new keyword, `field`, that accesses the compiler generated backing field in a property accessor.",
808
809
"_csharplang/proposals/unbound-generic-types-in-nameof.md": "This proposal introduces the ability to use unbound generic types such as `List<>` in `nameof` expressions. The type argument isn't required.",
809
810
"_csharplang/proposals/first-class-span-types.md": "This proposal provides several implicit conversions to `Span<T>` and `ReadOnlySpan<T>` that enable library authors to have fewer overloads and developers to write code that resolves to faster Span based APIs",
811
+
"_csharplang/proposals/simle-lambda-parameters-with-modifiers.md": "This proposal provides allows lambda parmaeters to be declared with modifiers without requiring their type names. You can add modifiers like `ref` and `out` to lambda parameters without specifying their type.",
810
812
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 7.md": "Learn about any breaking changes since the initial release of C# 10 and included in C# 11",
811
813
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 8.md": "Learn about any breaking changes since the initial release of C# 11 and included in C# 12",
812
814
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 9.md": "Learn about any breaking changes since the initial release of C# 12 and included in C# 13",
Copy file name to clipboardexpand all lines: docs/csharp/language-reference/compiler-messages/params-arrays.md
+6-1
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,7 @@ f1_keywords:
17
17
- "CS9225"
18
18
- "CS9227"
19
19
- "CS9228"
20
+
- "CS9272"
20
21
helpviewer_keywords:
21
22
- "CS0225"
22
23
- "CS0231"
@@ -33,7 +34,8 @@ helpviewer_keywords:
33
34
- "CS9225"
34
35
- "CS9227"
35
36
- "CS9228"
36
-
ms.date: 05/20/2024
37
+
- "CS9272"
38
+
ms.date: 02/13/2025
37
39
---
38
40
# Errors and warnings related to the `params` modifier on method parameters
39
41
@@ -57,6 +59,7 @@ That's by design. The text closely matches the text of the compiler error / warn
57
59
-[**CS9225**](#other-params-errors): *Constructor leaves required member uninitialized.*
58
60
-[**CS9227**](#parameter-and-argument-type-rules): *Type does not contain a definition for a suitable instance `Add` method.*
59
61
-[**CS9228**](#parameter-and-argument-type-rules): *Non-array params collection type must have an applicable constructor that can be called with no arguments.*
62
+
-[**CS9272**](#other-params-errors): *Implicitly typed lambda parameter cannot have the 'params' modifier.*
60
63
61
64
## Method declaration rules
62
65
@@ -101,6 +104,7 @@ The following errors indicate other issues with using the `params` modifier:
101
104
-**CS9223**: *Creation of params collection results in an infinite chain of invocation of constructor.*
102
105
-**CS9224**: *Method cannot be less visible than the member with params collection.*
103
106
-**CS9225**: *Constructor leaves required member uninitialized.*
107
+
-**CS9272**: *Implicitly typed lambda parameter cannot have the 'params' modifier.*
104
108
105
109
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.
106
110
@@ -111,6 +115,7 @@ The compiler generates one of the final three errors in the preceding list when
111
115
- The compiler emits **CS9223** when the code emitted to create the collection also contains a params collection of the same type. Typically, the `Add` method takes a `params` collection of the same type.
112
116
- The compiler emits **CS9224** when the `Create` method for the collection type is less accessible than the method that takes the `params` parameter of the collection type.
113
117
- The compiler emits **CS9225** when the collection type has a required member and the parameterless constructor doesn't initialize that member and have the <xref:System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttribute?displayProperty=nameWithType> on the parameterless constructor.
118
+
- The compiler emits **CS9272** when you've used the `params` modifier without type information on a lambda expression. You must specify the types for all lambda expression parameters to use the `params` modifier.
Copy file name to clipboardexpand all lines: docs/csharp/language-reference/operators/lambda-expressions.md
+10-10
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "Lambda expressions - Lambda expressions and anonymous functions"
3
3
description: C# lambda expressions that are used to create anonymous functions and expression bodied members.
4
-
ms.date: 11/22/2024
4
+
ms.date: 02/13/2025
5
5
helpviewer_keywords:
6
6
- "lambda expressions [C#]"
7
7
- "outer variables [C#]"
@@ -49,7 +49,7 @@ A lambda expression with an expression on the right side of the `=>` operator is
49
49
(input-parameters) =>expression
50
50
```
51
51
52
-
Thebodyofanexpressionlambdacanconsistofamethodcall. However, ifyou're creating [expression trees](../../advanced-topics/expression-trees/index.md) that are evaluated outside the context of the .NET Common Language Runtime (CLR), such as in SQL Server, you shouldn'tusemethodcallsinlambdaexpressions. Themethodshavenomeaningoutsidethecontextofthe .NETCommonLanguageRuntime (CLR).
52
+
Thebodyofanexpressionlambdacanconsistofamethodcall. However, whencreating [expressiontrees](../../advanced-topics/expression-trees/index.md) evaluatedbyaqueryprovider, limitmethodcallstothosemethodsrecognizedbythequeryprovider. Otherwise,thequeryprovidercan't replicate the method'sfunction.
53
53
54
54
## Statement lambdas
55
55
@@ -79,11 +79,11 @@ Two or more input parameters are separated by commas:
Inputparametertypesmustbeallexplicitorallimplicit; otherwise, a [CS0748](../compiler-messages/lambda-expression-errors.md#lambda-expression-parameters-and-returns) compilererroroccurs.
86
+
Inputparametertypesmustbeallexplicitorallimplicit; otherwise, a [CS0748](../compiler-messages/lambda-expression-errors.md#lambda-expression-parameters-and-returns) compilererroroccurs.BeforeC# 14, youmustincludetheexplicittypeonaparameterifithasanymodifiers, suchas `ref` or `out`. InC# 14, thatrestrictionisremoved. However, youmuststilldeclarethetypeifyouusethe `params` modifier.
87
87
88
88
Youcanuse [discards](../../fundamentals/functional/discards.md) tospecifytwoormoreinputparametersofalambdaexpressionthataren't used in the expression:
89
89
@@ -92,13 +92,13 @@ You can use [discards](../../fundamentals/functional/discards.md) to specify two
BeginningwithC# 12, youcanprovide*defaultvalues*forparameters on lambda expressions. The syntax and the restrictions on default parameter values are the same as for methods and local functions. The following example declares a lambda expression with a default parameter, then calls it once using the default and once with two explicit parameters:
97
+
BeginningwithC# 12, youcanprovide*defaultvalues*forexplicitly typed parameter lists. The syntax and the restrictions on default parameter values are the same as for methods and local functions. The following example declares a lambda expression with a default parameter, then calls it once using the default and once with two explicit parameters:
@@ -166,7 +166,7 @@ For more information about how to create and use async methods, see [Asynchronou
166
166
167
167
## Lambda expressions and tuples
168
168
169
-
The C# language provides built-in support for [tuples](../builtin-types/value-tuples.md). You can provide a tuple as an argument to a lambda expression, and your lambda expression can also return a tuple. In some cases, the C# compiler uses type inference to determine the types of tuple components.
169
+
The C# language provides built-in support for [tuples](../builtin-types/value-tuples.md). You can provide a tuple as an argument to a lambda expression, and your lambda expression can also return a tuple. In some cases, the C# compiler uses type inference to determine the types of tuple elements.
170
170
171
171
You define a tuple by enclosing a comma-delimited list of its components in parentheses. The following example uses tuple with three components to pass a sequence of numbers to a lambda expression, which doubles each value and returns a tuple with three components that contains the result of the multiplications.
172
172
@@ -299,7 +299,7 @@ var inc = [return: NotNullIfNotNull(nameof(s))] (int? s) => s.HasValue ? s++ : n
299
299
As the preceding examples show, you must parenthesize the input parameters when you add attributes to a lambda expression or its parameters.
300
300
301
301
> [!IMPORTANT]
302
-
> Lambda expressions are invoked through the underlying delegate type. That is different than methods and local functions. The delegate's `Invoke` method doesn't check attributes on the lambda expression. Attributes don't have any effect when the lambda expression is invoked. Attributes on lambda expressions are useful for code analysis, and can be discovered via reflection. One consequence of this decision is that the <xref:System.Diagnostics.ConditionalAttribute?displayProperty=nameWithType>cannot be applied to a lambda expression.
302
+
> Lambda expressions are invoked through the underlying delegate type. That invocation is different than methods and local functions. The delegate's `Invoke` method doesn't check attributes on the lambda expression. Attributes don't have any effect when the lambda expression is invoked. Attributes on lambda expressions are useful for code analysis, and can be discovered via reflection. One consequence of this decision is that the <xref:System.Diagnostics.ConditionalAttribute?displayProperty=nameWithType>can't be applied to a lambda expression.
303
303
304
304
## Capture of outer variables and variable scope in lambda expressions
305
305
@@ -309,7 +309,7 @@ Lambdas can refer to *outer variables*. These *outer variables* are the variable
309
309
310
310
The following rules apply to variable scope in lambda expressions:
311
311
312
-
- A variable that is captured won't be garbage-collected until the delegate that references it becomes eligible for garbage collection.
312
+
- A variable that is captured isn't garbage-collected until the delegate that references it becomes eligible for garbage collection.
313
313
- Variables introduced within a lambda expression aren't visible in the enclosing method.
314
314
- A lambda expression can't directly capture an [in](../keywords/method-parameters.md#in-parameter-modifier), [ref](../keywords/ref.md), or [out](../keywords/method-parameters.md#out-parameter-modifier) parameter from the enclosing method.
315
315
- A [return](../statements/jump-statements.md#the-return-statement) statement in a lambda expression doesn't cause the enclosing method to return.
Input parameters of a lambda expression are strongly typed at compile time. When the compiler can infer the types of input parameters, like in the preceding example, you may omit type declarations. If you need to specify the type of input parameters, you must do that for each parameter, as the following example shows:
24
+
Input parameters of a lambda expression are strongly typed at compile time. When the compiler can infer the types of input parameters, like in the preceding example, you can omit type declarations. If you need to specify the type of input parameters, you must do that for each parameter, as the following example shows:
25
25
26
-
[!code-csharp-interactive[specify types of input variables](snippets/shared/LambdaOperator.cs#ExplicitTypes)]
Copy file name to clipboardexpand all lines: docs/csharp/whats-new/csharp-14.md
+20
Original file line number
Diff line number
Diff line change
@@ -44,4 +44,24 @@ You can find the list of implicit span conversions in the article on [built-in t
44
44
45
45
Beginning with C# 14, the argument to `nameof` can be an unbound generic type. For example, `nameof(List<>)` evaluates to `List`. In earlier versions of C#, only closed generic types, such as `List<int>`, could be used to produce `List`.
46
46
47
+
## Simple lambda parameters with modifiers
48
+
49
+
Beginning with C# 14, you can add parameter modifiers, such as `scoped`, `ref`, `in`, `out`, or `ref readonly` to lambda expression parameters without specifying the parameter type:
Previously, adding any modifiers was allowed only when the parameter declarations included the types for the parameters. The preceding declaration would require typs on all parameters:
The `params` modifier still requires an explicitly typed parameter list.
64
+
65
+
You can read more about these changes in the article on [lambda expressions](../language-reference/operators/lambda-expressions.md#input-parameters-of-a-lambda-expression) in the C# language reference.
66
+
47
67
<!-- Add link to What's new in .NET 10 once it's published -->
0 commit comments