How to Put an Attribute on a Local Function in C#
A local function can carry attributes, just like a method: put them in square brackets on the line above it, or in front of a parameter. This post puts [Description] on a local function and on its parameter, and reads both back while the app runs.
A local function is a function declared inside another member. The C# guide: A local function is a named function declared inside another member, such as a method or constructor. Its name is in scope only within that member. An attribute adds information to it. The C# specification: An attribute is a piece of additional declarative information that is specified for a program entity.
Local functions and their parameters are among those entities. In the specification's list of attribute targets, the param target covers a parameter in a constructor, method, local function, and operator, and the specification adds: For an attribute on a local function declaration the default target is the local function.
Syntax
[<Attribute>(<arguments>)]
static <ReturnType> <Name>([<Attribute>(<arguments>)] <ParameterType> <parameter>) => <body>;
| Part | What it is |
|---|---|
[<Attribute>(<arguments>)] above the function | An attribute on the local function. It comes before static and the return type |
<Attribute> | The attribute, such as Description |
<arguments> | What the attribute takes, such as the description text |
static | Optional. A static local function can't use the variables around it |
<ReturnType> <Name> | What the function returns, and its name |
[<Attribute>(<arguments>)] before a parameter | An attribute on that parameter |
<ParameterType> <parameter> | The parameter's type and name |
<body> | The function's body: one expression after =>, or a block of statements |
The order comes from the C# specification's grammar, which starts a local function with attributes? local_function_modifier* return_type local_function_header: attributes first, then modifiers such as static, then the return type.
The Example
A console app called LocalFunctionAttribute has a local function, Shout, that turns words into capitals and adds an exclamation mark. Shout has a [Description] attribute, and so does its parameter. The app calls Shout once, then reads both descriptions back while it runs and prints them.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n LocalFunctionAttribute
cd LocalFunctionAttribute
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/LocalFunctionAttribute/LocalFunctionAttribute.csproj:
Determining projects to restore...
Restored /Users/you/source/LocalFunctionAttribute/LocalFunctionAttribute.csproj (in 47 ms).
Restore succeeded.
dotnet new console -n LocalFunctionAttribute creates a folder named LocalFunctionAttribute 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 LocalFunctionAttribute.csproj in Visual Studio.
Step 2: Write the Code
Replace everything in Program.cs. First the two using directives:
using System.ComponentModel;
using System.Reflection;
System.ComponentModel holds DescriptionAttribute, which [Description(...)] stands for. The reflection guide explains the short name: By convention, all attribute names end with the suffix "Attribute" to distinguish them from other types in the .NET libraries. However, you don't need to specify the attribute suffix when you use attributes in code. System.Reflection holds MethodInfo and the GetCustomAttribute method used below.
Then the call:
// Call the local function like any other method.
Console.WriteLine(Shout("good morning"));
Shout is declared at the end of the file, and the call works anyway. The C# specification: A local function may be called from a lexical point prior to its declaration.
Then the function's MethodInfo:
// A delegate that points to the local function gives you its MethodInfo.
Func<string, string> shout = Shout;
MethodInfo method = shout.Method;
Func<string, string> is a delegate type for a function that takes a string and returns a string, and shout points to Shout. shout.Method Gets the method represented by the delegate, in the words of its API reference. It's a MethodInfo, a class that Discovers the attributes of a method and provides access to method metadata.
Then the two attributes:
// Read the attribute on the function, then the one on its parameter.
Console.WriteLine($"Function: {method.GetCustomAttribute<DescriptionAttribute>()?.Description}");
Console.WriteLine($"Parameter: {method.GetParameters()[0].GetCustomAttribute<DescriptionAttribute>()?.Description}");
GetCustomAttribute<DescriptionAttribute>() Retrieves a custom attribute of a specified type that is applied to a specified member. It returns A custom attribute that matches T, or null if no such attribute is found, so ?.Description reads the text only when there is an attribute. method.GetParameters()[0] is the first parameter, words, and GetCustomAttribute reads its attribute the same way.
Last, the local function:
// A local function with an attribute on it, and another on its parameter.
[Description("Turns words into capitals and adds an exclamation mark.")]
static string Shout([Description("The words to shout.")] string words) => words.ToUpperInvariant() + "!";
The [Description(...)] line above static string Shout belongs to the function, and [Description("The words to shout.")] in front of string words belongs to the parameter. static is optional. The C# specification: A non-static local function may capture variables from the enclosing scope while a static local function shall not (so it has no access to enclosing locals, parameters, non-static local functions, or this). Shout needs nothing from around it, so it can be static.
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
GOOD MORNING!
Function: Turns words into capitals and adds an exclamation mark.
Parameter: The words to shout.

Shout("good morning") printed GOOD MORNING!. Then the app read both attributes back: the function's description, Turns words into capitals and adds an exclamation mark., and the parameter's, The words to shout. The attributes didn't change what Shout does: they're information that other code can read, the way the last two lines did.
The Complete File

using System.ComponentModel;
using System.Reflection;
// Call the local function like any other method.
Console.WriteLine(Shout("good morning"));
// A delegate that points to the local function gives you its MethodInfo.
Func<string, string> shout = Shout;
MethodInfo method = shout.Method;
// Read the attribute on the function, then the one on its parameter.
Console.WriteLine($"Function: {method.GetCustomAttribute<DescriptionAttribute>()?.Description}");
Console.WriteLine($"Parameter: {method.GetParameters()[0].GetCustomAttribute<DescriptionAttribute>()?.Description}");
// A local function with an attribute on it, and another on its parameter.
[Description("Turns words into capitals and adds an exclamation mark.")]
static string Shout([Description("The words to shout.")] string words) => words.ToUpperInvariant() + "!";
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Writing the attribute after static | static [Description("...")] string Shout(string words) stopped the build with error CS1031: Type expected | Put the attribute first, on the line above static string Shout |
Looking the function up by its name with GetMethod | typeof(Program).GetMethod("Shout", ...) returned null, because the compiler gave the local function another name (see the FAQ) | Get it through a delegate: Func<string, string> shout = Shout;, then shout.Method |
Building with LangVersion set to 8 | error CS8400: Feature 'local function attributes' is not available in C# 8.0. Please use language version 9.0 or greater. | Remove the LangVersion setting, so the project uses its default (see the FAQ) |
Putting [Conditional("DEBUG")] on a local function that isn't static | error CS8783: Local function 'Log(string)' must be 'static' in order to use the Conditional attribute | Make it static: [Conditional("DEBUG")] static void Log(string message) built and printed debug: starting |
FAQ
What name does the compiler give a local function?
Not the one you wrote. typeof(Program).GetMethod("Shout", BindingFlags.NonPublic | BindingFlags.Static) returned null, and the delegate's Method.Name printed <<Main>$>g__Shout|0_0. That's why the example gets the MethodInfo through a delegate.
Which C# version do I need?
C# 9 or later, as the CS8400 error says: Please use language version 9.0 or greater. A .NET 10 project has that already. The language versioning reference lists C# 14 as the default for .NET 10.x, and: If you remove the LangVersion element from your project file, the compiler uses the default for your target framework.
Conclusion
Write an attribute on a local function the way you write one on a method: in square brackets on the line above, before static and the return type, or in front of a parameter. It needs C# 9 or later, which a .NET 10 project has by default. To read it back, get the function's MethodInfo through a delegate and call GetCustomAttribute.
Additional Resources
- Local functions — what a local function is and where you can declare one.
- Attribute specification — the attribute targets, local functions and their parameters included.
- Attributes and reflection — how to write attributes and read them back.
CustomAttributeExtensions.GetCustomAttribute— reading one attribute of a given type.- C# language versioning — the default C# version for each .NET version.
