首页 > 编程语言 >C# break 和 return的区别

C# break 和 return的区别

时间:2022-08-20 22:56:45浏览次数:50  
标签:return C# System break WriteLine using Console

下面示例是break的用法:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Drawing;
 6 using System.Windows.Forms;
 7 
 8 namespace ReturnDemo
 9 {
10     class Kodify_Example
11     {
12         static void Main()
13         {
14             for (int i = 1; i <= 7; i++)
15             {
16 
17                 for (int j = 1; j <= 7; j++)
18                 {
19                     int product = i * j;
20 
21                     if (product >= 20)
22                     {
23                         break;
24                     }
25                     Console.Write("{0}\t", product);
26                 }
27                 Console.WriteLine("\n");
28                 Console.Write("I am inner");
29                 Console.WriteLine("\n");
30             }
31             Console.WriteLine("\nFinished with calculations.");
32         }
33     }
34 }

 

 由上面示例可见,每次j循环达到break语句条件时,break只是跳出了最内部的循环,外部循环继续运行,i=1直到i=7全部循环完毕。

下面是return的用法:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Drawing;
 6 using System.Windows.Forms;
 7 
 8 namespace ReturnDemo
 9 {
10     class Kodify_Example
11     {
12         static void Main()
13         {
14             for (int i = 1; i <= 7; i++)
15             {
16 
17                 for (int j = 1; j <= 7; j++)
18                 {
19                     int product = i * j;
20 
21                     if (product >= 20)
22                     {
23                         return;
24                     }
25                     Console.Write("{0}\t", product);
26                 }
27                 Console.WriteLine("\n");
28                 Console.Write("I am inner");
29                 Console.WriteLine("\n");
30             }
31             Console.WriteLine("\nFinished with calculations.");
32         }
33     }
34 }

 

 

由上面示例可见,第一次达到触发returnt条件语句时,return就跳转到整个void Main()函数的外层,就连Console.WriteLine("\nFinished with calculations.");这条语句都没有显示出来。

综合上述:

break仅导致从循环退出,所以任何循环之后的语句都会执行。另一方面,return会导致从当前函数体中退出,所以函数体内的语句不会再执行。

所以,在触发语句之后你想要退出当前函数体,就用return;如果你想要继续在函数体中执行,使用break.

break causes exit from the loop only, so any statements after loop will be executed. On the other hand, return causes exit from the current function, so no further statements inside this function will be executed.

So - if you want to exit current function after finding the first element, use return. If you want to continue execution in this function, use break.

标签:return,C#,System,break,WriteLine,using,Console
From: https://www.cnblogs.com/chenlight/p/16608978.html

相关文章

  • React报错之React Hook useEffect has a missing dependency
    正文从这开始~总览当useEffect钩子使用了一个我们没有包含在其依赖数组中的变量或函数时,会产生"ReactHookuseEffecthasamissingdependency"警告。为了解决该错误,禁......
  • Vulfocus靶场 | Django SQL注入(CVE-2021-35042)
    漏洞复现这题通关比较简单,但是django这个漏洞还挺出名的,之前在ctf上也遇到过相关的题目\ 直接按照提示,进入了Django报错页面 flag就在里面,直接ctrl+F就能找到......
  • Vulfocus靶场 | webmin 远程代码执行 (CVE-2019-15642)
    描述Webmin是一套基于Web的用于类Unix操作系统中的系统管理工具。Webmin1.920及之前版本中的rpc.cgi文件存在安全漏洞。攻击者可借助特制的对象名称利用该漏洞执行代码......
  • 根据MySQL表生成C#实体模型
    1USEINFORMATION_SCHEMA;2SELECT3CONCAT(4'///<summary>\r\n///',5COLUMN_COMMENT,6'\r\n///</summary>\r\n[DataMemb......
  • cartographer-位姿推测部分
    下面就详细讲解一下外推器是怎么推算位姿的。先看一下PoseExtrapolator的实现:解释一下它的功能:保持某个时间段内的位姿以估计线速度和角速度;用速度来推测运动;如果使用了I......
  • Vulfocus靶场 | Druid 任意文件读取(CVE-2021-36749)
    漏洞原理由于用户指定HTTPInputSource没有做出限制,可以通过将文件URL传递给HTTPInputSource来绕过应用程序级别的限制。由于ApacheDruid默认情况下是缺乏授权......
  • Vulfocus靶场 | Metabase geojson任意文件读取漏洞(CVE-2021-41277)
    漏洞描述etabase是一个开源数据分析平台。在受影响的版本中,已发现自定义GeoJSON地图(admin->settings->maps->custommaps->addamap)支持和潜在的本地文件包含(包括环......
  • Vulfocus靶场 | GoCD 任意文件读取漏洞 (CVE-2021-43287)
    漏洞描述GoCD一款先进的持续集成和发布管理系统,由ThoughtWorks开发。(不要和Google的编程语言Go混淆了!)其前身为CruiseControl,是ThoughtWorks在做咨询和交付交付项目时......
  • 360se_getdbkey_c++
    关联:360安全浏览器历史记录、收藏夹  下载链接:https://files.cnblogs.com/files/DirWang/360se_getdbkey.zip?t=1661006095......
  • Typora+picgo+github图片及文件上传
    一、本地文件上传github右上角newrepository复制https连接在本地下载:git然后找到你要上传的文件夹项目,右键点击文件夹(注意:不能选单个文件或者压缩包......