What is the Empty statement
In C#, an empty statement is a statement that consists of just a semicolon ;. It is used when you want to have a statement that does nothing. It can be helpful in certain scenarios where you need to provide a placeholder or indicate that no action is required at that point.
Here's an example that demonstrates the use of an empty statement in C#:
int x = 5;
if (x > 10)
{
Console.WriteLine("x is greater than 10.");
}
else
{
;
}
In the code above, we have an if-else statement. If the condition x > 10 is true, the message "x is greater than 10." will be printed to the console. However, if the condition is false, we have an empty statement ; inside the else block. The empty statement indicates that no action is required in the else branch.
In this particular example, the empty statement could be omitted altogether, and the code would function the same way. However, there may be situations where the presence of an empty statement can help improve code readability or maintain consistency in code structure.
It's important to note that while an empty statement does nothing, it still has an impact on program flow. For example, if you have a loop that includes an empty statement, it will still consume CPU cycles, even though it doesn't perform any actual work. Therefore, it's essential to use empty statements judiciously and consider whether they are truly necessary in your code.
If we use it in while loop where we just have blank body and label statements:
void CallEmptyExample()
{
while(print())
{
; //empty statement
}
}
bool print()
{
Console.WriteLine("www.dotnetustad.com");
return true;
}
As you can see that infinite loop will be processed. So for termination press CTRL + C in case of console application.