r/csharp•
ap2016

How to more efficiently handle repeating switch statements?

Hello all! Say I have an Apple, Banana, and Orange. I also have a bunch of methods that need to know what type of fruit is being passed so that they can do what they need to do. For example, PickFruit(Apple) would execute a little bit differently than PickFruit(Orange).

My question is: how do I stop my code from having switch statements in every single method, trying to discern what type of fruit it is dealing with? Let's say that I cannot make a new class of Fruit that know the methods.

Right now I'm passing type of fruit repeatedly. Thanks!

Posted Apr 26, 2017, 6:13 PM UTC

1010
Loading comments…