Вопрос по c# – Когда следует попытаться исключить оператор switch? [Дубликат]

8

This question already has an answer here:

Is Switch (Case) always wrong? 8 answers

Error: User Rate Limit ExceededError: User Rate Limit Exceeded, Однако, прочитавнесколько посты на stackoverflow озамена Error: User Rate Limit Exceeded Error: User Rate Limit ExceededError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

private MyType DoSomething(IDataRecord reader)
{
    var p = new MyType
                {
                   Id = (int)reader[idIndex],
                   Name = (string)reader[nameIndex]
                }

    switch ((string) reader[discountTypeIndex])
    {
        case "A":
            p.DiscountType = DiscountType.Discountable;
            break;
        case "B":
            p.DiscountType = DiscountType.Loss;
            break;
        case "O":
            p.DiscountType = DiscountType.Other;
            break;
    }

    return p;
}

Error: User Rate Limit Exceeded

Update:Error: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded Amir Afghani
Error: User Rate Limit Exceeded jrcs3
Error: User Rate Limit Exceeded mezoid
Error: User Rate Limit Exceeded bbqchickenrobot

Ваш Ответ

13   ответов
2

Error: User Rate Limit Exceeded mezoid
Error: User Rate Limit Exceeded
2

Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededThe the Open/Closed principal:

If the mapping is likely to changeError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

3

Error: User Rate Limit Exceeded mezoid
Error: User Rate Limit Exceeded
1

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

public enum DiscountType
{
    None,

    [Description("A")]
    Discountable,

    [Description("B")]
    Loss,

    [Description("O")]
    Other
}

public GetDiscountType(string discountTypeIndex)
{
    foreach(DiscountType type in Enum.GetValues(typeof(DiscountType))
    {
        //Implementing GetDescription should be easy. Search on Google.
        if(string.compare(discountTypeIndex, GetDescription(type))==0)
            return type;
    }

    throw new ArgumentException("DiscountTypeIndex " + discountTypeIndex + " is not valid.");
}
14

private DiscountType GetDiscountType(string discount)
{
    switch (discount)
    {
        case "A": return DiscountType.Discountable;
        case "B": return DiscountType.Loss;
        case "O": return DiscountType.Other;
    }
}
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded mezoid
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
0

Error: User Rate Limit Exceeded

private MyType DoSomething(IDataRecord reader)
{
    var p = new MyType
                {
                   Id = (int)reader[idIndex],
                   Name = (string)reader[nameIndex]
                }

    p.DiscountType = FindDiscountType(reader[discountTypeIndex]);

    return p;
}

private DiscountType FindDiscountType (string key) {
    switch ((string) reader[discountTypeIndex])
    {
        case "A":
            return DiscountType.Discountable;
        case "B":
            return DiscountType.Loss;
        case "O":
            return DiscountType.Other;
    }
    // handle the default case as appropriate
}

Error: User Rate Limit Exceeded

-1

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

1

Error: User Rate Limit ExceededifError: User Rate Limit ExceededifError: User Rate Limit ExceededswitchError: User Rate Limit ExceededswitchError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

if (string) reader[discountTypeIndex]) == "A")
   p.DiscountType = DiscountType.Discountable;
else if (string) reader[discountTypeIndex]) == "B")
   p.DiscountType = DiscountType.Loss;
else if (string) reader[discountTypeIndex]) == "O")
   p.DiscountType = DiscountType.Other;

Error: User Rate Limit ExceededswitchError: User Rate Limit Exceeded

1

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededifError: User Rate Limit Exceeded

public enum DiscountType : int
{
    Unknown = 0,
    Discountable = 1,
    Loss = 2,
    Other = 3
}

Error: User Rate Limit Exceeded

p.DiscountType = Enum.Parse(typeof(DiscountType), 
    (string)reader[discountTypeIndex]));

Error: User Rate Limit Exceeded

2

private static Dictionary<string, DiscountType> DiscountTypeLookup = 
  new Dictionary<string, DiscountType>(StringComparer.Ordinal)
    {
      {"A", DiscountType.Discountable},
      {"B", DiscountType.Loss},
      {"O", DiscountType.Other},
    };

1

p.Discount = DiscountFactory.Create(reader[discountTypeIndex]);

Error: User Rate Limit Exceeded mezoid
15

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded mezoid
0

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Похожие вопросы