Named parameters in Func<T>/Action<T> type in C#?
If you ever wondered, why Visual Studio (and Visual Studio for Mac) suggest you to name your lambda arguments with names like arg1, arg2 … argN and so on…
Here is a common situation where somewhere in your library or helper class you have a method taking in
and then when you want to use this method VS suggests you following names: arg1 and arg2.
Like there is nothing better than that :)
There is a cheap and easy solution for that.
- Define your own delegate with same signature as
Func<T1,T2,T3>you are using already.
- Specify intended names for these arguments (in my case arg1 could be better described as service, where arg2 is actually a token).
- In original method, replace Func
with this new delegate.
After these changes when you are going to use the method, you will see intellisense suggesting correct names and not generic ones.
Pros and Cons
- You, your team and other people using the API that you expose (whether internally in the app or in the library or NuGet) will see better names and probably will use these names.
- So code will be easier to read and maintain.
- If you decide to extend the delegate, you will have to do it in one place only.
- It’s fun to have smart code suggestions!
- Bit of extra work and extra code
You can have optional
In the example below you can omit
token argument completely.