Goto statement in C#

In this article I will explain How to use go to statement in C#.
  • 2838

Introduction

The goto statement is a jump statement in C#. Goto statement is used transfers the control directly to a labeled statement. The goto statement is also useful to get out of deeply nested loops.

Example

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace goto_statement

{

    class statement

    {

        static void Main(string[] args)

        {

            string name;

            int marks;

            label: //creating label with colon(:)

            Console.WriteLine("Enter your name:");

            name = Console.ReadLine();

            Console.WriteLine("Welcome {0}", name);

            Console.WriteLine("Enter your Marks:");

            marks = Convert.ToInt32(Console.ReadLine());

            goto label; //jump to label statement          

        }

    }

}

 

The output of following program
  

Clipboard187.jpg

© 2019 DotNetHeaven. All rights reserved.