ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C# break 和 return的区别

2022-08-20 23:03:09  阅读:190  来源: 互联网

标签: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
来源: https://www.cnblogs.com/chenlight/p/16608978.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有