Is it possible to reorganize this C # if (..) operator?

simple question: -

i have the following simple if (..) operators: -

if (foo == Animal.Cat || foo == Animal.Dog) { .. } if (baa == 1|| baa == 69) { .. } 

you can reorganize them into something like ...

DISCLAIMER: I know this is not compiling .. but this is what I am trying to get ...

 if (foo == (Animal.Cat || Animal.Dog)) { .. } if (baa == (1 || 69)) { .. } 

Greetings :)

EDIT

I wonder if lambda expression extension can do this ?: P

+7
if-statement refactoring
source share
7 answers

Usually I create an extension method IsIn() , which takes a general array of parameters of type <T> , and then calls .Contains() on it, passing the instance to which the extension is called.

It looks like if (foo.IsIn(foo1, foo2)) . Very easy to write, very easy to use.

+13
source share

In this particular case, this is not so much if you have only two options, but if there can be many parameters that you may prefer, enter the parameters in the list, and then use Contains as follows:

 List<Animal> allowedAnimals = new List<Animal> { Animal.Cat, Animal.Dog, Animal.Fish, Animal.Frog, Animal.Horse }; if (allowedAnimals.Contains(animal)) { // etc... } 

This method will also work with types other than enums , including types that you cannot use switch . This is also useful if the list of valid values ​​is known only at run time.

Please note that if you create a new list every time there will be a penalty for performance.

+3
source share

To do this, you can use the extension method:

 public static bool IsAny<T>(this Enum value, params T[] values) { foreach (T e in values) { if (value.Equals(e)) return true; } return false; } 

using:

 enum TestEnum { Stackoverflow, overflowStack, stackstack } TestEnum val = TestEnum.overflowStack; Console.WriteLine(val.IsAny(TestEnum.stackstack, TestEnum.Stackoverflow)); //false Console.WriteLine(val.IsAny(TestEnum.overflowStack, TestEnum.Stackoverflow)); // true 
+2
source share

You can use switch with failure (ugly legacy of C syntax):

 switch (foo) { case Animal.Cat: case Animal.Dog: { ... break; } } 

Will only work for such trivial examples, and in any case, I suggest sticking with if .

+1
source share

I don’t know much about .NET 4.0 (now I use 3.5), but I think there is no way to achieve the effect of Pure.Krome.

But an approximation to such a comparison can be achieved using arrays and extension methods that LINQ provides.

Here is a small example.

 string foo = @"very weird"; if (new[] { @"don't do it", @"very weird" }.Contains(foo)) { Console.WriteLine(@"bingo string"); } int baa = 7; if (new []{5, 7}.Contains(baa)) { Console.WriteLine(@"bingo int"); } 

It is output:

 bingo string bingo int 

Thus, more resources are consumed than a simple chain of companions and logical operators, but provides a syntax similar to the one Pure.Krome that it wants to receive.

If you do not want to define arrays with a new [] (this is really curious, but in logical conditions), you can define your extension method for this.

 public static class Extensions { public static bool IsOneOf<T>(this T obj, params T[] args) { return args.Contains(obj); } } 

So you can use this extension method:

 if (baa.IsOneOf(5, 7, 9, 10)) { Console.WriteLine(@"bingo int"); } 

The conclusion is really predictable, heh.

+1
source share

Another way to do this is to separate the rules and logic that must be followed when all / all of them are true:

 var animal_rules = new Func<Animal, bool>[]{ (a) => a == Animal.Cat, (a) => a == Animal.Dog }; var baa_rules = new Func<int, bool>[]{ (b) => b == 1, (b) => b == 69 } 

Then you can use the rule with .Any () and / or All () to perform the appropriate operations, for example:

 if(animal_rules.Any(rule => rule(foo)){ // animal logic here... } if(baa_rules.Any(rule => rule(baa)){ // baa logic here... } 

NTN

0
source share

I think the best question is why bother with this at all? It is clear that he is standing. I bet if you posted more of your code, there would be many other things that should be addressed first.

0
source share

All Articles