Best way to check for null parameters (Guard Clauses)

后端 未结 10 2006
北荒
北荒 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:14

    If you want to save typing the argument name twice, like Guard.AgainstNull(arg, nameof(arg));

    check out YAGuard, where you can write Guard.AgainstNull(arg);

    No need to specify the name of the argument in the guard clause, but in the argument thrown, the name is correctly resolved.

    It also supports guard-and-set in the form MyProperty = Assign.IfNotNull(arg);

    Nuget: YAGuard

    Disclaimer: I'm the author of YAGuard.

提交回复
热议问题