14

i am trying to use switch case instead of If Else statement, in which i have to first check length of string and as per that i have to make cases of it.

switch (mystring.length)
{
    case <=25:
    {
        //do this
        break;
    }
    case <50:
    {
        //do this
        break;
    }
    default:
        break;
}

This is some thing i want to do but unable to get how to put <25 in front of case because it is not appropriate as per switch case rules.

Uthistran Selvaraj.
  • 558
  • 2
  • 11
  • 29
neerajMAX
  • 269
  • 3
  • 4
  • 14
  • 4
    Why do you want to use `switch` for this task? Why is `if` bad for you? – horgh Dec 18 '12 at 06:57
  • http://www.dotnetperls.com/switch Switch case requires each case be constant.... – Uthistran Selvaraj Dec 18 '12 at 06:59
  • Please, read [MSDN switch (C# Reference)](http://msdn.microsoft.com/en-us/library/06tc147t%28v=vs.90%29.aspx). – horgh Dec 18 '12 at 07:01
  • @KonstantinVasilcov : i dont have any problem with "If" i am just trying either it is possible or not. – neerajMAX Dec 18 '12 at 07:04
  • @kovilpattiCsharper : i know that why i mentioned it is not possible as per switch case rules. – neerajMAX Dec 18 '12 at 07:05
  • @neerajMAX it's not...Any implementation of this task through `switch` would be at least less readeble and hardly better than `if` – horgh Dec 18 '12 at 07:06
  • @KonstantinVasilcov : sorry to say but it is some how possible just check Habib's answer... :) – neerajMAX Dec 18 '12 at 07:23
  • yeah, but it's still not a good solution. Just because it can be done doesn't mean it should be used. I would still use an `if else if` – default Dec 18 '12 at 07:25
  • 1
    @neerajMAX it's not a straightforward usage of `switch`, but a workaround.... I see no sense in it while having `if`-statement. Why doing simple things difficult? – horgh Dec 18 '12 at 07:29
  • @deafult: ofcourse it is not a appropriate solution. but atleast it is possible... and you never know might be it will help you in future... :P B +ve & Think +ve :D – neerajMAX Dec 18 '12 at 07:30
  • @KonstantinVasilcov : to make thing intresting... although i am not going to use it in any of my solution... :D – neerajMAX Dec 18 '12 at 07:32

4 Answers4

22

Its always better to use if/else for your particular case, With switch statement you can't put conditions in the case. It looks like you are checking for ranges and if the range is constant then you can try the following (if you want to use switch statement).

int Length = mystring.Length;
int range = (Length - 1) / 25;
switch (range)
{
    case 0:
        Console.WriteLine("Range between 0 to 25");
        break;
    case 1:
        Console.WriteLine("Range between 26 to 50");
        break;
    case 2:
        Console.WriteLine("Range between 51 to 75");
        break;

}
Habib
  • 219,104
  • 29
  • 407
  • 436
  • what if range = 22/25 ??????? it will go to the default case instead of first case. – neerajMAX Dec 18 '12 at 07:10
  • 3
    @neerajMAX, if the Length is `22` then it will go to the first case, since `(22 - 1) / 25` is `0`, its integer division :) – Habib Dec 18 '12 at 07:11
  • 1
    I would be very confused if I saw this in code. I would question the use of range and why an if hasn't been used. – default Dec 18 '12 at 07:24
  • @Default, that is why I said to use `if/else`, this solution is only here because it is possible using a switch statement if the range is constant. Just one possible way, never said its the better way to do :) – Habib Dec 18 '12 at 07:25
  • @Default nobody using it... it was just a thought came in my mind.. so i shared it with all the intelligent people over here... :) – neerajMAX Dec 18 '12 at 07:28
  • 1
    @Habib oh.. right at the start, I completely missed that. better get some coffee.. – default Dec 18 '12 at 07:28
19

This really doesn't help the OP much, but hopefully it will help someone looking for this in the future.

If you're using C# 7 (Available in Visual Studio 2017), you can switch on a range.

Example:

switch (mystring.length)
{
    case int n when (n >= 0 && n <= 25):
    //do this
    break;

    case int n when (n >= 26 && n <= 50 ):
    //do this
    break;
}
Steve
  • 11,596
  • 7
  • 39
  • 53
2

You can not do this with switch but there may be a workaround for this.

Dictionary<int, Action> actions = new Dictionary<int, Action>()
    {
        {25,()=>Console.WriteLine("<25")},
        {49,()=>Console.WriteLine("<50")},
        {int.MaxValue,()=>Console.WriteLine("Default")},
    };


actions.First(kv => mystring.length < kv.Key).Value();
I4V
  • 34,891
  • 6
  • 67
  • 79
  • 1
    This solution can be extended to be more general. The first element of Tuple can be of type Func to provide more generic match. – Dmitrii Lobanov Dec 18 '12 at 07:05
  • 1
    I don't think this is reliable. [There is no order defined for `Dictionary`s](http://stackoverflow.com/a/4007787/382780), so any valid pair could be picked, not just the "first". This should probably be a list of `Tuple` so that there is order to the evaluation. – 31eee384 Jul 27 '15 at 20:39
2

Try this:

int range = (int) Math.Floor(mystring.Length / 25);

switch (range) {
case 0:

    //do this <= 25
    break;

case 1:

    //do this < 50 & > 25
    break;

default:
    break;
}​
palaѕн
  • 72,112
  • 17
  • 116
  • 136