473,405 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

switch statement bug

I tested following program gives runtime error:
Unhandled Exception: System.InvalidProgramException:
Common Language Runtime detected an invalid program.

using System;
public class SwitchTest
{
static void Main()
{
const int i = 1;
switch (i)
{
case 0:
Console.WriteLine
(0);
goto default;
case 1:
Console.WriteLine
(1);
goto case 0;
default:
Console.WriteLine
("all others");
break;
}
}
}

However, by rearranging the case section, this bug is
avoided:

public class SwitchTest
{
static void Main()
{
const int i = 1;
switch (i)
{
case 1:
Console.WriteLine
(1);
goto case 0;
case 0:
Console.WriteLine
(0);
goto default;
default:
Console.WriteLine
("all others");
break;
}
}
}

According to documentation, since C# has a "no fall
through" rule to ensure the order of case doesn't matter,
I think this is a bug.
Nov 15 '05 #1
2 1465
Fall through definitely isn't supported - to prove this, leave a break
statement out of any of the cases and it won't compile.

I think if you switch it to int i = 1 instead of const that you won't have a
problem. The fact that there isn't fall through doesn't necessarily mean
that the ordering doesn't matter. Logically I think that's true, but
architecturally, I'm not sure that's so b/c if you take out the const, you
don't have a problem.
"fred" <a7******@telus.net> wrote in message
news:01****************************@phx.gbl...
I tested following program gives runtime error:
Unhandled Exception: System.InvalidProgramException:
Common Language Runtime detected an invalid program.

using System;
public class SwitchTest
{
static void Main()
{
const int i = 1;
switch (i)
{
case 0:
Console.WriteLine
(0);
goto default;
case 1:
Console.WriteLine
(1);
goto case 0;
default:
Console.WriteLine
("all others");
break;
}
}
}

However, by rearranging the case section, this bug is
avoided:

public class SwitchTest
{
static void Main()
{
const int i = 1;
switch (i)
{
case 1:
Console.WriteLine
(1);
goto case 0;
case 0:
Console.WriteLine
(0);
goto default;
default:
Console.WriteLine
("all others");
break;
}
}
}

According to documentation, since C# has a "no fall
through" rule to ensure the order of case doesn't matter,
I think this is a bug.

Nov 15 '05 #2
fred wrote:
I tested following program gives runtime error:
Unhandled Exception: System.InvalidProgramException:
Common Language Runtime detected an invalid program.

using System;
public class SwitchTest
{
static void Main()
{
const int i = 1;
switch (i)
{
case 0:
Console.WriteLine
(0);
goto default;
case 1:
Console.WriteLine
(1);
goto case 0;
default:
Console.WriteLine
("all others");
break;
}
}
}


Apparently, this bug has been fixed in Framework version 1.1.

--
mikeb

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a...
35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
17
by: prafulla | last post by:
Hi all, I don't have a copy of C standard at hand and so anyone of you can help me. I have always wondered how switch statements are so efficient in jumping to the right case (if any)? Can...
5
by: Alvin Bruney | last post by:
Is a switch more efficient than an if statement? I observe thru the debugger that a switch statement jumps directly to its case handler where as an if statement examines all conditions...
3
by: pgraeve | last post by:
I am a convert from VB to C# so bear with me on this "conversion" question C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. ...
27
by: Yuriy Solodkyy | last post by:
Hi VS 2005 beta 2 successfully compiles the following: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
25
by: v4vijayakumar | last post by:
'continue' within switch actually associated with the outer 'while' loop. Is this behavior protable? int ch = '\n'; while (true) { switch(ch) { case '\n': cout << "test"; continue; } }
12
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
2
by: Phillip B Oldham | last post by:
What would be the optimal/pythonic way to subject an object to a number of tests (based on the object's attributes) and redirect program flow? Say I had the following: pets = {'name':...
13
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.