个性化阅读
专注于IT技术分析

C# goto语句

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!
赞(0)
未经允许不得转载:srcmini » C# goto语句

评论 抢沙发

评论前必须登录!