C#教程
C#控制语句
C#函数
C#数组
C#面向对象
C#命名空间和异常
C#文件IO
C#集合
C#多线程
C#其它

C# Goto语句

C# Goto语句

C# goto 语句也称为跳转语句。它用于将控制权转移到程序的其他部分。无条件跳转到指定标签。
可用于从深层嵌套循环或switch case标签转移控制。
目前,在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!
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4