C# Method Generator
Describe a method and get idiomatic C#: the right access and static modifiers, async Task or Task<T> with the Async suffix and a CancellationToken, optional parameters with defaults, guard clauses using the .NET 7 and 8 ThrowIf helpers, an expression body where it fits, and XML documentation with summary, param, returns and exception tags.
- Runs in your browser
- No sign-up
- Free to use
How to use C# Method Generator
- Enter the method name and its class.
- List the parameters as name: type = default.
- Set the return type and choose async, guards and other options.
- Copy the method and implement it.
C# Method Generator features
Async done right
Task<T>, Async suffix and CancellationToken.
Guard clauses
ThrowIfNull, ThrowIfNullOrWhiteSpace and ThrowIfNegative.
Optional parameters
Constant defaults; non-constant ones become null.
XML docs
summary, param, returns and exception tags.
Expression bodies
=> for simple methods without guards.
Usings
System, Collections.Generic, Threading and Tasks as needed.
When to use C# Method Generator
- Scaffolding service methods in ASP.NET Core.
- Writing documented public APIs for NuGet libraries.
- Standardising argument validation.
- Learning async and documentation conventions in C#.
C# Method Generator FAQ
Why add a CancellationToken?
It lets callers cancel long operations, for example when an HTTP request is aborted. Pass it on to every awaited call that accepts one.
Why does the name end in Async?
It is the .NET convention for methods that return Task, so callers can see at a glance that they must await the call.
Which .NET version do the guard helpers need?
ArgumentNullException.ThrowIfNull needs .NET 6; ThrowIfNullOrWhiteSpace and ThrowIfNegative need .NET 7 or 8. The notes show the classic alternative.
Why did a default become null?
C# default parameter values must be compile-time constants, so dates and lists cannot be defaults. The parameter becomes nullable and you assign the real default in the method.
When is an expression body used?
When the option is ticked and the method has no guard clauses and is not async, it is written as a single expression after =>.
Is anything uploaded?
No. The method is generated in your browser.
Idiomatic C# methods
C# methods follow strong conventions that make code bases predictable: PascalCase names, Async suffixes for awaitable methods, cancellation tokens as the last parameter, guard clauses at the top and XML documentation for anything public. The generator applies those conventions to a method you describe in a few lines.
Parameters use neutral types that become C# types, and names are converted to camelCase. Optional parameters get default values when C# allows it. Because defaults must be compile-time constants, a “now” or empty list default becomes a nullable parameter with null as default, and the notes explain how to assign the real value.
Asynchronous methods return Task or Task<T>, are named with the Async suffix and can take a CancellationToken that defaults to default, so callers can ignore it. The placeholder body awaits Task.Yield() so the method compiles without warnings until the real awaited calls are added.
Guard clauses protect the method from invalid input. Recent .NET versions provide static helpers that throw the right exception with the parameter name filled in automatically: ThrowIfNull for references, ThrowIfNullOrWhiteSpace for strings and ThrowIfNegative for counts. The XML documentation lists the exceptions the guards may throw.
For short methods that compute a single value, an expression body (=>) is more concise; the generator uses it when the method has no guards and is not async. The result is wrapped in a class with the necessary using directives.