I think the pinpoint exact answer to your question is like CodeMaster already indicated at if statements matching multiple values
That being said, since you are checking for a DialogResult
, in this specific case, I doubt performance considerations come into play and we look more into code readability (as you likely won't popup 10,000 dialogs to the user).
The simplest solution is a switch statement to increase readability:
switch(fAuth.ShowDialog(this.DialogParent))
{
case DialogResult.Cancel:
case DialogResult.Abort:
case DialogResult.None:
case DialogResult.No:
return;
default:
break;
}
The problem with that is that you possibly have to duplicate code if you want to check the same thing in multiple places. In that case I would suggest to wrap this into a function like that (change the name to your liking):
public class DialogEvaluator
{
public static bool IsResultNegative(DialogResult result)
{
switch(fAuth.ShowDialog(this.DialogParent))
{
case DialogResult.Cancel:
case DialogResult.Abort:
case DialogResult.None:
case DialogResult.No:
return true;
default:
return false;
}
}
}
//And use it like that:
if(DialogEvaluator.IsResultNegative(fAuth.ShowDialog(this.DialogParent))
{
return;
}
If you have however a bunch of different cases with different requirements, then I would go back again to the Generic In
Method and wrap the possible outcomes in well named List
s:
public static class DialogEvaluator
{
public static bool In<T>(this T obj, IEnumerable<T> args)
{
return args.Contains(obj);
}
static DialogEvaluator
{
NegativeResult = new List<DialogResult>() { DialogResult.Cancel, DialogResult.Abort, DialogResult.None, DialogResult.No };
SpecificNegativeResult = new List<DialogResult>() { DialogResult.Cancel, DialogResult.Abort, DialogResult.No };
}
public static List<DialogResult> NegativeResult {get; private set;}
public static List<DialogResult> SpecificNegativeResult {get; private set;}
}
//And use it like that:
if (fAuth.ShowDialog(this.DialogParent)
.In(DialogEvaluator.NegativeResult)
{
return;
}
//Or
if (fAuth.ShowDialog(this.DialogParent)
.In(DialogEvaluator.SpecificNegativeResult)
{
return;
}
It depends on your circumstances and you have to balance readability, maintainability and conciseness.