Home > OS >  Guard Method for Integer C#
Guard Method for Integer C#

Time:11-08

I'm trying to check if a parameter that was received is 0, but as I'm doing this alot I would like to have a faster way to check if it is 0 and not to have to do an entire if check every time.

Exactly like the Guard method allow me to do that with string. This Guard method: using CommunityToolkit.Diagnostics;

Here's an example code:

 Guard.IsNotNullOrEmpty(myname);
 Guard.IsNotNullOrEmpty(yourname);

 //this works for strings and if that string is null or empty it will generate an Execption for me

 if(myage == 0 )
             {
                 throw new Exception("Your age cannot be 0");
             }

 //this does check if myage is 0, but it took 3 lines of code`


There is something like Guard for integers?

CodePudding user response:

I've used this project which you can get on nuget also: https://github.com/safakgur/guard

it encompasses multi requirement in one line, plus logging.

CodePudding user response:

You can use Guard.IsNotDefault(source):

Guard.IsNotDefault(myage);
  • Related