Alex Sorokoletov Notes     About     Archive     Feed

Named parameters in Func/Action 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 Func<T> or Action<T>. Definition of your method using Func/Action classes and then when you want to use this method VS suggests you following names: arg1 and arg2. Like there is nothing better than that :) Definition of your method using Func/Action classes

There is a cheap and easy solution for that.

  • Define your own delegate with same signature as Func<T> or 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.

Defining custom delegate with correct names and using it in the original method After these changes when you are going to use the method, you will see intellisense suggesting correct names and not generic ones. Now you get better names from VS as a suggestion

Read more...