Best way to check for null parameters (Guard Clauses)

后端 未结 10 2037
北荒
北荒 2021-01-31 16:45

For example, you usually don\'t want parameters in a constructor to be null, so it\'s very normal to see some thing like

if (someArg == null)
{
    throw new Arg         


        
10条回答
  •  面向向阳花
    2021-01-31 17:26

    With newer version of C# language you can write this without additional library or additional method call:

    _ = someArg ?? throw new ArgumentNullException(nameof(someArg));
    _ = otherArg ?? throw new ArgumentNullException(nameof(otherArg));
    

提交回复
热议问题