Home > Blockchain >  C# change bool type in tuple when specific Error type is occurred
C# change bool type in tuple when specific Error type is occurred

Time:10-22

I have this Tuple

return new Tuple<bool, List<Error>, List<Error>>(false, errors, validErr);

I want to set bool to true if error type is TimeoutException

I tried to solve problem like this

TimeoutException err = null

if(err is TimeoutException )

return new Tuple<bool, List<Error>, List<Error>>(true, errors, validErr);
}
else {
return new Tuple<bool, List<Error>, List<Error>>(false, errors, validErr);
}

I am not sure if this is correct or not. Any suggestions please?

CodePudding user response:

use ternary operator to achieve it in less line of code.

 TimeoutException err = null
 bool hasTimeoutException = err is TimeoutException ? true : false;
 return new Tuple<bool, List<Error>, List<Error>>(hasTimeoutException, errors, validErr);

or you can directly add that ternary operator in return statement like below:

 return new Tuple<bool, List<Error>, List<Error>>(err is TimeoutException ? true : false, errors, validErr);
  •  Tags:  
  • c#
  • Related