C# Empty Statement(;)

In this chapter you will learn:
  • What is Empty Statement in C#?
  • How to use Empty Statement in C#?
  • What is the benefit of using Empty Statement in C#?

An empty statement is used when you no need to perform an operation where a statement is required. It simply transfers control to the end point of the statement. It is also very useful with a while loop with the blank body and label statements.

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace empty_statements
{
    class Program
    {
        public bool print()
        {
            Console.WriteLine("Steven Clark");
            return true;
        }
        static void Main(string[] args)
        {
            int i = 0;
            Program p = new Program();
            while (p.print())
            {
                ; //Empty Statement
            }
            Console.WriteLine("i = {0}", i);
            Console.ReadLine();
        }
    }
}

  Note: It processes infinite loop so terminating the program press ctrl+c.

Output

Steven Clark
Steven Clark
Steven Clark
Steven Clark
Steven Clark
Steven Clark
Steven Clark
Steven Clark
Steven Clark
Steven Clark
Steven Clark
Steven Clark
Steven Clark
Steven Clark
Steven Clark
__

Summary

In this chapter you learned about empty statement in C#. You also learned how to use empty statement in C# programming. In next chapter you will learn about goto statement in C#.

 

Share your thought