How to Put an Attribute on a Lambda Parameter in C#
A lambda's parameter can carry an attribute, just like a method's parameter. Write it inside the lambda's parentheses, right in front of the parameter: ([Description(...)] double celsius) => ....
Microsoft's reference on lambda expressions: You can add attributes to a lambda expression and its parameters. What they do, and don't do: 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. That's why the example reads its attribute back with reflection.
Syntax
<DelegateType> <name> = ([<Attribute>(<arguments>)] <ParameterType> <parameter>) => <expression>;
| Part | What it is |
|---|---|
<DelegateType> | The type of the variable that holds the lambda, such as Func<double, double> |
[<Attribute>(<arguments>)] | The attribute, in square brackets, in front of the parameter it belongs to |
<ParameterType> <parameter> | The parameter. Its type can be left out when <DelegateType> already gives it |
( ) | The parentheses around the parameters. With an attribute, they're required |
<expression> | What the lambda works out and returns |
The reference on the parentheses: you must parenthesize the input parameters when you add attributes to a lambda expression or its parameters.
The Example
A console app called LambdaAttribute has a lambda that turns degrees Celsius into degrees Fahrenheit. Its parameter carries a Description attribute that says what the number means, and the program reads that description back.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n LambdaAttribute
cd LambdaAttribute
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/LambdaAttribute/LambdaAttribute.csproj:
Determining projects to restore...
Restored /Users/you/source/LambdaAttribute/LambdaAttribute.csproj (in 39 ms).
Restore succeeded.
dotnet new console -n LambdaAttribute creates a folder named LambdaAttribute with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or LambdaAttribute.csproj in Visual Studio.
Step 2: Write the Code
Replace everything in Program.cs. First the lambda, with the attribute on its parameter:
using System.ComponentModel;
using System.Reflection;
// The attribute goes in front of the parameter, inside the parentheses.
Func<double, double> toFahrenheit =
([Description("A temperature in degrees Celsius")] double celsius) => celsius * 9 / 5 + 32;
using System.ComponentModel;brings inDescriptionAttribute, an attribute that holds a text, andusing System.Reflection;the types that read attributes.[Description("A temperature in degrees Celsius")]sits inside the lambda's parentheses, in front ofdouble celsius, so it belongs to that parameter.celsius * 9 / 5 + 32is the conversion itself.
Then call the lambda:
Console.WriteLine($"100 C is {toFahrenheit(100)} F");
toFahrenheit(100) runs the lambda like a method, with 100 as celsius.
Last, read the attribute back:
// The attribute is stored with the lambda's parameter, and reflection can read it.
ParameterInfo parameter = toFahrenheit.Method.GetParameters()[0];
DescriptionAttribute? description = parameter.GetCustomAttribute<DescriptionAttribute>();
Console.WriteLine($"Parameter {parameter.Name}: {description?.Description}");
toFahrenheit.Methodis the method the compiler made from the lambda. The .NET reference: Gets the method represented by the delegate.GetParameters()returns its parameters, and[0]is the first one,celsius.GetCustomAttribute<DescriptionAttribute>()Retrieves a custom attribute of a specified type that is applied to a specified parameter. It returnsnullwhen there's none, so the variable is aDescriptionAttribute?and the print usesdescription?.Description.
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
100 C is 212 F
Parameter celsius: A temperature in degrees Celsius

The lambda turned 100 into 212, and reflection found the attribute on the parameter named celsius, with its text, A temperature in degrees Celsius. The attribute played no part in the calculation: it's information stored with the parameter, for code that looks for it.
The Complete File

using System.ComponentModel;
using System.Reflection;
// The attribute goes in front of the parameter, inside the parentheses.
Func<double, double> toFahrenheit =
([Description("A temperature in degrees Celsius")] double celsius) => celsius * 9 / 5 + 32;
Console.WriteLine($"100 C is {toFahrenheit(100)} F");
// The attribute is stored with the lambda's parameter, and reflection can read it.
ParameterInfo parameter = toFahrenheit.Method.GetParameters()[0];
DescriptionAttribute? description = parameter.GetCustomAttribute<DescriptionAttribute>();
Console.WriteLine($"Parameter {parameter.Name}: {description?.Description}");
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Leaving out the parentheses | [Description("A temperature in degrees Celsius")] celsius => celsius * 9 / 5 + 32 didn't build: error CS8916: Attributes on lambda expressions require a parenthesized parameter list. | Put the parameter in parentheses, even when it's the only one |
| Expecting the attribute to check the argument | With ([Range(1, 5)] int count) => new string('*', count), a call with 8 printed eight stars: nothing checked the range | Check the value in the lambda's body. The reference: Attributes don't have any effect when the lambda expression is invoked. |
| An attribute that isn't allowed on parameters | ([Obsolete] double celsius) => ... didn't build: error CS0592: Attribute 'Obsolete' is not valid on this declaration type. It is only valid on 'class, struct, enum, constructor, method, property, indexer, field, event, interface, delegate' declarations. | Use an attribute that's allowed on parameters. The compiler errors guide: Each attribute's AttributeUsageAttribute specifies which declaration types it targets. |
FAQ
Do I have to write the parameter's type?
No. With the type left out, ([Description("A temperature in degrees Celsius")] celsius) => celsius * 9 / 5 + 32 built, printed 212, and reflection read the same description: Func<double, double> already says that celsius is a double. Only the parentheses are required.
Conclusion
To put an attribute on a lambda's parameter, write it in front of the parameter, inside the parentheses. It changes nothing when the lambda runs: it's there for the code that reads it, through reflection or code analysis.
Additional Resources
- Lambda expressions: attributes — attributes on a lambda and on its parameters.
- Errors and warnings for lambda expressions — CS8916 and the other lambda errors.
- Delegate.Method — the method behind a lambda.
- CustomAttributeExtensions.GetCustomAttribute — reading an attribute.
