C#拾遗补漏之goto跳转语句

前言

在我们日常工作中常用的C#跳转语句有break、continue、return,但是还有一个C#跳转语句很多同学可能都比较的陌生就是goto,今天大姚带大家一起来认识一下goto语句及其它的优缺点。

goto语句介绍

  • goto 语句由关键字 goto 后跟一个标签名称组成,通过标签名称指定跳转的位置。

  • 可以在方法的任何地方放置标签,并且可以多次使用相同的标签。

goto代码使用示例

使用goto进行代码重试示例

        /// <summary>
        /// 使用goto进行代码重试示例
        /// </summary>
        public static void GotoRetryUseExample()
        {
            int retryCount = 0;
            for (int i = 0; i < 10; i++)
            {
            retryLogic:
                try
                {
                    //模拟可能出错的操作
                    Random random = new Random();
                    int result = random.Next(0, 2);

                    if (result == 0)
                    {
                        throw new Exception("Error occurred");
                    }

                    Console.WriteLine("Operation successful on attempt: " + retryCount);
                }
                catch (Exception ex)
                {
                    retryCount++;
                    if (retryCount < 3)
                    {
                        Console.WriteLine("Error occurred, retrying...");
                        goto retryLogic; //跳转到重试逻辑
                    }
                    else
                    {
                        Console.WriteLine("Max retry limit reached.");
                        return;
                    }
                }
            }
        }

不使用goto进行代码重试示例

        /// <summary>
        /// 不使用goto进行代码重试示例
        /// </summary>
        public static void NonGotoRetryUseExample()
        {
            int retryCount = 0;
            for (int i = 0; i < 10; i++)
            {
                while (retryCount < 3)
                {
                    try
                    {
                        //模拟可能出错的操作
                        Random random = new Random();
                        int result = random.Next(0, 2);

                        if (result == 0)
                        {
                            throw new Exception("Error occurred");
                        }

                        Console.WriteLine("Operation successful on attempt: " + retryCount);
                        break;
                    }
                    catch (Exception ex)
                    {
                        retryCount++;
                        Console.WriteLine("Error occurred, retrying...");
                    }
                }

                if (retryCount == 3)
                {
                    Console.WriteLine("Max retry limit reached.");
                    return;
                }
            }
        }

goto正常输出使用示例

        /// <summary>
        /// goto正常输出使用示例
        /// </summary>
        public static void GotoGeneralUseExample(int num)
        {
            if (num < 0)
            {
                goto LessThanZero;
            }
            else if (num == 0)
            {
                goto EqualToZero;
            }
            else
            {
                goto GreaterThanZero;
            }

        LessThanZero:
            Console.WriteLine("数字小于零");
            goto End;

        EqualToZero:
            Console.WriteLine("数字等于零");
            goto End;

        GreaterThanZero:
            Console.WriteLine("数字大于零");
            goto End;
        End:
            Console.WriteLine("End...");
        }

不使用goto正常输出使用示例

        /// <summary>
        /// 不使用goto正常输出使用示例
        /// </summary>
        public static void NonGotoGeneralUseExample(int num)
        {
            if (num < 0)
            {
                Console.WriteLine("数字小于零");
            }
            else if (num == 0)
            {
                Console.WriteLine("数字等于零");
            }
            else
            {
                Console.WriteLine("数字大于零");
            }
            Console.WriteLine("End...");
        }

goto语句的优缺点

通过上述代码示例我们可以总结如下goto语句的几大优缺点,大家可以根据自己的使用场景谨慎合理的使用。

优点:

  1. 简化复杂逻辑: 在某些情况下,goto 可以帮助简化复杂的逻辑流程,减少嵌套结构。

  2. 跳出多层循环: 可以用于直接跳出多层循环,避免使用额外的标志变量。

缺点:

  1. 降低可读性: 过度使用 goto 可能会导致代码难以理解,降低代码的可读性。

  2. 增加维护难度: goto 可能使代码结构复杂化,增加代码的维护难度。

  3. 潜在引入bug: 不当使用 goto 可能会引入潜在的错误,打破正常的控制流程。

相关推荐
5967851545 小时前
DotNetty ChannelRead接收数据为null
tcp/ip·c#
weixin_464078076 小时前
C#串口温度读取
开发语言·c#
明耀8 小时前
WPF RadioButton 绑定boolean值
c#·wpf
一丝晨光10 小时前
Java、PHP、ASP、JSP、Kotlin、.NET、Go
java·kotlin·go·php·.net·jsp·asp
Death20010 小时前
Qt 中的 QListWidget、QTreeWidget 和 QTableWidget:简化的数据展示控件
c语言·开发语言·c++·qt·c#
Death20011 小时前
Qt 3D、QtQuick、QtQuick 3D 和 QML 的关系
c语言·c++·qt·3d·c#
yufei-coder11 小时前
C#基础语法
开发语言·c#·.net
yngsqq11 小时前
031集——文本文件按空格分行——C#学习笔记
笔记·学习·c#
Mudrock__12 小时前
前后端传输文件(图片)
vue·.net
新手unity自用笔记1 天前
项目-坦克大战学习-子弹的移动与销毁
笔记·学习·c#