C # - shorter version of IF statement

Is there a shorter version of the IF statement?

if (el.type == ElementType.Type1 || el.type == ElementType.Type2)
+3
source share
13 answers

You can use the extension method, but will it be much better?

Drop this on a static class:

public static bool IsOneOf(this ElementType self, params ElementType[] options)
{
    return options.Contains(self);
}

And then you can do:

if (el.type.IsOneOf(ElementType.Type1, ElementType.Type2)) {

However, this will be much slower than your if statement, since there is an implicit array initialization followed by an array traversal, unlike (no more) two comparisons and branches.

+9
source

Consider ElementType as

enum ElementType
{
Type1,
Type2,
Type3
}

In this particular case, you can write if(el.type<ElementType3) By default, Type1 is 0, Type2 is 1, etc.

+1
source

2 , , , , , , (IMHO).

, switch:

switch (el.type)
{
    case ElementType.Type1:
    case ElementType.Type2:
    case ElementType.Type3:
        //code here
        break;
    case ElementType.Type4:
    case ElementType.Type5:
        //code here
        break;
    case ElementType.Type6:
        //code here
        break;
}

if statements:

if (el.type == ElementType.Type1 ||
    el.type == ElementType.Type2 ||
    el.type == ElementType.Type3 )
{
    // code here
}else if(el.type == ElementType.Type4 ||
         el.type == ElementType.Type5)
{
    // code here
}else if(el.type == ElementType.Type6)
{
    // code here
}

, switch /, ( "" ):)

+1

:

if(new [] { ElementType.Type1, ElementType.Type2 }.Contains(el.type))

(, )

0

, IN() ? ... , ... - :

if ((new [] { ElementType.Type1, ElementType.Type2}).Contains(el.type)) {...}

( ), , .

if (el.type == ElementType.Type1 | el.type == ElementType.Type2)

, . - , .

0

- . #, . , , , , if. . OR.

0

. , if if.

if, , , ,

(X > [Y || Z || A])

, if (X > Y || X > Z || X > A)

( )

0

: ( ). .

if 5 ;)

bool b = (el.type == ElementType.Type1) | (el.type == ElementType.Type2);

if(b){...}
0

, , .

private bool isType1OrType2(ElementType type)
{
    return type == ElementType.Type1 || type == ElementType.Type2;
}

if(isType1OrType2(el.type))

public static bool isType1OrType2(this ElementType type)
{
    return type == ElementType.Type1 || type == ElementType.Type2;
}

if(el.type.isType1OrType2())

, , . , , , . , , .

0

,

0

, , .

enum MyEnum
{
    A,
    B,
    C

}
private readonly Dictionary<MyEnum, Action> _handlers = new Dictionary<MyEnum, Action>
                                                        {
    {MyEnum.A,()=>Console.Out.WriteLine("Foo")},
    {MyEnum.B,()=>Console.Out.WriteLine("Bar")},
    };

public static void ActOn(MyEnum e)
{
    Action handler = null;
    if (_handlers.TryGetValue(e, out handler) && handler != null)
    {
        handler();
    }
}
0

, .

    private void ActWithCast(MyEnum e)
    {
        const int interest = (int)MyEnum.A | (int)MyEnum.B;
        if (0 != ((int)e & interest))
        {
            Console.Out.WriteLine("Blam");
        }
    }
0

ElementType , :

[Flags]
public enum ElementType
{
    Type1 = 1,
    Type2 = 2,
    Type3 = 4,
}
...
tElementType.HasFlag(ElementType.Type1 | ElementType.Type2);

You do not need the [Flags] attribute to use HasFlag, but the values ​​of each must follow this pattern.

0
source

Source: https://habr.com/ru/post/1779411/


All Articles