Forcing named arguments in C#

前端 未结 5 1295
忘了有多久
忘了有多久 2021-02-05 00:49

C# 4 introduced a feature called named arguments which is especially useful in scenarios like

int RegisterUser(string nameFirst, string nameLast, string nameMidd         


        
5条回答
  •  生来不讨喜
    2021-02-05 01:27

    Sorry for a shameless plug!
    I implemented a Roslyn analyzer to enforce using named arguments for a method.

    So if you install the RequireNamedArgs analyzer and add a special comment before a method that should be invoked with named arguments:

    //[RequireNamedArgs]
    int RegisterUser(string nameFirst, string nameLast, string nameMiddle, string email)
    

    The analyzer will emit an error if a caller attempts to use positional arguments instead of named.

    Take a look at it in action:

    If you decide to give it a go -- do so at your own risk :)

提交回复
热议问题