what is "fallthrough" situation in C#?
Hi,
I am last year B.Sc.IT student.I had learn C#.In my last exam in C# paper there is question like "what is "fallthrough" situation in C#?".I had never heard this kind of term in C#.That's why I asked this question in this forum. If you have any idea about this please help me.
thanks in advanced.
Re: what is "fallthrough" situation in C#?
Fallthrough is a situation that is use in switch statement to go from one case to another or go to next case. C# does not allow automatic fallthrough situation in a switch statement. You can enable this situation using goto statement. In C# if you are able to write your goto statement to go to the next case statement, then it is said that you achieve fallthrough.
Below is the example of fallthough situation:
Code:
switch ("ramesh")
{
case "ramesh":
Console.WriteLine ("hello ramesh");
goto case "amit";
case "amit":
Console.WriteLine (" hello amit");
break;
default:
break;
}
Re: what is "fallthrough" situation in C#?
In other language what happened that in switch statement if your case is match then only that case is executed and you come out of the switch statement.In C# if your switch case is match then you can go to another case using fallthrough situation.Here below is the example of fallthrough situation.See how control is passed from one case to another case.
Code:
switch (1)
{
case A:
Console.WriteLine ("you are in case A");
goto case B;
case B:
Console.WriteLine ("you are in case B");
break;
default:
break;
Re: what is "fallthrough" situation in C#?
fallthrough situation:fallthrough is very useful concept in C# language.You can activate fallthrough situation by using 'goto <case label>' statement. It specify you where exactly you want to go.Break statement is used to break switch statement and come out of the switch statement.
Code:
switch(value)
{
case Apple:
Console.WriteLine("In case Apple\n");
goto case Mango;
case Mango:
Console.WriteLine("In case Mango\n");
goto case banana;
case banana:
Console.WriteLine("In case banana\n");
break;
}