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!