Can a `default` case also be in the middle or on the beginning of a switch?

Advertisements

I have some switch-case blocks. Which do have a default case.

The default is normally placed at the "end" of the switch section. Like in ths example

switch(n)
{
    case(1): 
        // code for case 1 here...
        break;
    case(3): 
        // code for case 3 here...
        break;
    case(2):
    default: 
        // code here is for default and case 2
        break;
}

I know that the case (2) could be ommit, but we in the team decided to keep all possible cases in the switch.

On the other-side I would love to have the order of 1, 2 and 3.

So my question would be, can I move the default in the middle of the switch? I found on msdn – the-switch-statement that the default can be placed everywhere.

The default case can appear in any place within a switch statement. Regardless of its position, the default case is always evaluated last and only if all other case patterns aren’t matched, except if goto default is encountered.

switch(n)
{
    case(1): 
        // code for case 1 here...
        break;
    case(2):
    default: 
        // code here is for default and case 2
        break;
    case(3): 
        // code for case 3 here...
        break;
}

Or would it be possible even possible to change the order or case(2) and default?

switch(n)
{
    case(1): 
        // code for case 1 here...
        break;
    default: 
    case(2):
        // code here is for default and case 2
        break;
    case(3): 
        // code for case 3 here...
        break;
}

The last code snippet, keeps the "order" and also shows that "2" is same as default. I like that most for readability.

My question, is the placement of "default" in the middle of the switch block save? Or was that introduced in later c# version?

From the comments … I added my test-method

        [TestMethod]
        public void SwitchMethodWithDefaultsInMiddle()
        {
            Func<int, string> func = (n) => {
                string ret = "";
                switch (n)
                {
                    case (1):
                        // code for case 1 here...
                        ret = "case 1";
                        break;
                    default:
                    case (2):
                        // code here is for default and case 2
                        ret = "case 2 or default";
                        break;
                    case (3):
                        // code for case 3 here...
                        ret = "case 3";
                        break;
                }
                return ret;
            };

            Assert.AreEqual("case 1", func(1));
            Assert.AreEqual("case 2 or default", func(2));
            Assert.AreEqual("case 3", func(3));

            Assert.AreEqual("case 2 or default", func(0));
            Assert.AreEqual("case 2 or default", func(4));
        }

May I do rephrase my question: Is that code "ok" for all c# versions, not only for my current one? My current code should be part of code-docu / guide-lines, so may someone with older VS or C# version is picking it up.

>Solution :

The ECMA 334 C# specification 1st edition from 2001 has an example of exactly this on p187, section 15.7.2:

32 switch (i) {
33 default:
34    CaseAny();
35    break;
36 case 1:
37    CaseZeroOrOne();
38    goto default;
39 case 0:
40    CaseZero();
41    goto case 1;
42 } 

so: it has always been valid

Leave a Reply Cancel reply