迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 >

在 C# 中退出函数

作者:迹忆客 最近更新:2024/02/01 浏览次数:

本文将介绍如何在 C# 中退出函数。

跳转语句一般用于控制程序执行的流程。换句话说,跳转语句在执行程序中无条件地将控制从一个点转移到另一个点。

以下是分类为 Jump 语句的 C# 中的五条语句。

  1. break 语句;
  2. continue 语句;
  3. goto 语句;
  4. return 语句;
  5. throw 语句。

在 C# 中使用 break 语句退出函数

break 语句在它存在的地方停止循环。然后,如果可用,控件将传递到终止语句之后的语句。

如果嵌套循环中存在 break 语句,它只会终止那些包含 break 语句的循环。

例子:

// C# program to illustrate the
// use of break statement
using System;

class Test {
  // Main Method
  static public void Main() {
    int[] Numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
    foreach (int number in Numbers) {
      // print only the first 10 numbers
      if (number > 10) {
        break;
      }
      Console.Write($"{number} ");
    }
  }
}

输出:

1 2 3 4 5 6 7 8 9 10

在 C# 中使用 continue 语句退出函数

当某个条件为真时,continue 语句会跳过代码块的执行。与 break 语句不同,continue 语句将控制转移到循环的开头。

下面是使用 foreach 方法的代码示例。

// C# program to illustrate the
// use of continue statement
using System;

class Test {
  // Main Method
  static public void Main() {
    int[] Numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
    foreach (int oddNumber in Numbers) {
      // print only the odd numbers 10 numbers
      if (oddNumber % 2 == 0) {
        continue;
      }
      Console.Write($"{oddNumber} ");
    }
  }
}

输出:

1 3 5 7 9 11 13 15 17 19

在 C# 中使用 goto 语句退出函数

我们使用 goto 语句将控制转移到程序中的标记语句。标签必须是放在 goto 语句之前的有效标识符。

换句话说,它强制执行标签上的代码。

在下面的示例中,goto 语句强制执行案例 5。

// C# program to illustrate the
// use of goto statement
using System;

class Test {
  // Main Method
  static public void Main() {
    int age = 18;
    switch (age) {
      case 5:
        Console.WriteLine("5yrs is less than the recognized age for adulthood");
        break;
      case 10:
        Console.WriteLine("Age 10 is still underage");
        break;
      case 18:
        Console.WriteLine("18yrs! You are now an adult and old enough to drink");
      // goto statement transfer
      // the control to case 5
      goto case 5; default:
        Console.WriteLine("18yrs is the recognized age for adulthood");
        break;
    }
  }
}

输出:

18yrs! You are now an adult and old enough to drink
5yrs is less than the recognized age for adulthood

在 C# 中使用 return 语句退出函数

return 语句终止它出现的函数执行,然后将控制权返回给调用方法的结果(如果可用)。但是,如果函数没有值,则使用 return 语句而不使用表达式。

例子:

// C# program to illustrate the
// use of return statement
using System;

class Test {
  // Main Method
  static public void Main() {
    int[] Numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
    foreach (int number in Numbers) {
      // print only the first 10 numbers
      if (number > 10) {
        return;
      }
      return;
      Console.Write($"{number} ");
    }
  }
}

输出:

No output

在 C# 中使用 throw 语句退出函数

异常表明发生了错误或改变了程序的执行。throw 语句使用 new 关键字创建一个有效 Exception class 的对象。

所有 Exception 类都有 Stacktrace 和 Message 属性。

请注意,有效异常必须从 Exception 类派生。有效的异常类包括 ArgumentExceptionInvalidOperationExceptionNullReferenceExceptionIndexOutOfRangeException

例子:

// C# program to illustrate the
// use of throw statement
using System;

class Test {
  // Main Method
  static public void Main() {
    int[] Numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
    foreach (int number in Numbers) {
      // using try catch block to
      // handle the Exception
      try {
        // print only the first 10 numbers
        if (number > 10) {
          Console.WriteLine();
          throw new NullReferenceException("Number is greater than 10");
        }
        Console.Write($"{number} ");
      } catch (Exception exp) {
        Console.WriteLine(exp.Message);
        return;
      }
    }
  }
}

输出:

1 2 3 4 5 6 7 8 9 10
Number is greater than 10

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

在 C# 中发出 HTTP POST Web 请求

发布时间:2024/02/04 浏览次数:131 分类:编程语言

在 C# 中,可以使用 3 种主要方法来发出 HTTP POST Web 请求:WebClient 类,HttpWebRequest 类和 HttpClient 类。本教程将讨论在 C# 中发出 HTTP POST Web 请求的方法。使用 C# 中的 WebClient 类发出 HTTP POST Web 请求

在 C# 中调整图像大小

发布时间:2024/02/04 浏览次数:203 分类:编程语言

有两种主要方法可用于在 C# 中调整图像的大小,Bitmap 类构造函数和 graphics.DrawImage()函数。在本教程中,我们将讨论在C#中调整图像大小的方法。我们将带您完成整个过程,从加载原始图像到保

在 C# 中下载图片

发布时间:2024/02/04 浏览次数:138 分类:编程语言

有 3 种主要方法可用于下载 C# 中的图片,WebClient.DownloadFile()函数,Bitmap 类和 Image.FromStream()函数。在 C# 中使用 WebClient 类下载图片 WebClient 类提供了用于向 C# 中的 URL 发送数据和从 URL 接收数据

在 C# 中使用秒表

发布时间:2024/02/04 浏览次数:139 分类:编程语言

我们可以使用 Stopwatch 类来计算 C# 中的经过时间。使用 C# 中的秒表类计算经过时间 Stopwatch 类在 C# 中准确测量经过的时间。

在 C# 中获取可执行路径

发布时间:2024/02/04 浏览次数:200 分类:编程语言

有 3 种主要方法可用于获取 C# 中程序的可执行路径,即 Assembly 类,AppDomain 类和 Path 类。本教程将介绍获取 C# 代码的可执行路径的方法。使用 C# 中的 Assembly 类获取可执行路径

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便