A more elegant way to replace a switch statement in C #

Source: Internet
Author: User

Today in the project encountered the use of switch statements to determine the condition, but the problem is more conditions, about dozens of conditions, full screen case judgment, whether there is a more elegant alternative to the switch statement?


Suppose there is a scenario where the shopping malls often take different discounts according to the situation, and if the discount scheme is less, consider using a switch statement for judgment. But if there are dozens of or even hundreds of discount schemes, using the switch statement is not elegant enough.

First, a discount interface.

    public interface Ivalueprocessor
    {
        Decimal Dazhe (short policy,decimal orginprice);
    }

The formal parameter policy is used to receive enumeration items about discounting, and the formal parameter orginprice represents the price before the discount. The enumeration entries for discounting are:

    public enum Policyenum
    {
        Wuzhe = 0,
        Liuzhe = 1,
        Qizhe = 2,
        Bazhe = 3,
        
    }

Implement Ivalueprocessor interface, take different algorithm according to different policyenum items.

   public class Myvalueprocessor:ivalueprocessor
    {
        Public decimal Dazhe (short policy,decimal orginprice)
        {
            Switch (Policy)
            {
                Case (short) Policyenum.wuzhe:
                    return ORGINPRICE/2;
                Case (short) Policyenum.liuzhe:
                    Return Orginprice * (decimal) 0.6;
                Case (short) Policyenum.qizhe:
                    Return Orginprice * (decimal) 0.7;
                Case (short) Policyenum.bazhe:
                    Return Orginprice * (decimal) 0.8;
                Case (short) Policyenum.jiuzhe:
                    Return Orginprice * (decimal) 0.9;
                Default
                    return ORGINPRICE/2;
            }
        }
    }

The client calls are as follows:

        static void Main (string[] args)
        {
            Console.WriteLine ("Please enter the discount policy, 0 means 50 percent, 1 means 60 percent, 2 means 70 percent, 3 means 80 percent, 4 means 90 percent:");
            String policy = Console.ReadLine ();
            Decimal originprice = (decimal) 100.00;
            Console.WriteLine ("The price before the discount is:" + originprice);
            Myvalueprocessor processor = new Myvalueprocessor ();
            Console.WriteLine ("Price After Discount:" + processor. Dazhe (short. Parse (Policy), originprice));
            Console.readkey ();
        }

The above is not too big question, whether there is a replacement switch judgment, a more elegant way of writing it?

In the Dazhe (short Policy,decimal Orginprice) method of the Myvalueprocessor class, receives a parameter of type short and a formal parameter of type decimal, returning the decimal type, inside the method, The parameter of the short type is used as the judging condition of the switch statement, and then the return value is obtained using different algorithms. Can be further abstracted: the short type as a key in the dictionary collection, the algorithm, that is, the delegate as a dictionary collection of value. In this way, we can encapsulate various discount schemes in the Dictionary collection. Modify the following:

    public class Myvalueprocessor:ivalueprocessor
    {
        Private ReadOnly Dictionary<shortFunc<decimaldecimal> > _dic;
        Public Myvalueprocessor ()
        {
            _dic = new Dictionary<shortFunc<decimaldecimal>
            
                {0, M = = m * (decimal) 0.5},
                {1, M = = m * (decimal) 0.6},
                {2, M = = m * (decimal) 0.7},
                {3, M = = m * (decimal) 0.8},
                {4, M = = m * (decimal) 0.9}
            };
        }
        Public decimal Dazhe (short policy,decimal orginprice)
        {
            if (_dic. ContainsKey (Policy))
            {
                Return _dic[policy]. Invoke (Orginprice);
            }
            return ORGINPRICE/2;
        }
    }

Thus, within the Dazhe (short Policy,decimal orginprice) method, it is only possible to determine if the incoming short type argument is a key to the dictionary collection.

A more elegant way to replace a switch statement in C #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.