📜  C#Goto语句

📅  最后修改于: 2020-10-31 02:04:54             🧑  作者: Mango

C#转到语句

C#goto语句也称为跳转语句。它用于将控制权转移到程序的其他部分。它无条件跳转到指定的标签。

它可用于从深层嵌套的循环或开关案例标签转移控制权。

当前,避免在C#中使用goto语句,因为它会使程序变得复杂。

C#Goto语句示例

让我们看一下C#中goto语句的简单示例。

using System;
public class GotoExample
    {
      public static void Main(string[] args)
      {
      ineligible:
          Console.WriteLine("You are not eligible to vote!");

      Console.WriteLine("Enter your age:\n");
      int age = Convert.ToInt32(Console.ReadLine());
      if (age < 18){
              goto ineligible;
      }
      else
      {
              Console.WriteLine("You are eligible to vote!"); 
      }
      }
   }

输出:

You are not eligible to vote!
Enter your age:
11
You are not eligible to vote!
Enter your age:
5
You are not eligible to vote!
Enter your age:
26
You are eligible to vote!