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 可能会引入潜在的错误,打破正常的控制流程。

相关推荐
军训猫猫头4 小时前
96.如何使用C#实现串口发送? C#例子
开发语言·c#
不爱写代码的玉子5 小时前
HALCON透视矩阵
人工智能·深度学习·线性代数·算法·计算机视觉·矩阵·c#
开开心心就好8 小时前
高效Excel合并拆分软件
开发语言·javascript·c#·ocr·排序算法·excel·最小二乘法
一名用户10 小时前
unity实现自定义粒子系统
c#·unity3d·游戏开发
钢铁男儿12 小时前
C# 类和继承(扩展方法)
java·servlet·c#
爱炸薯条的小朋友12 小时前
C#由于获取WPF窗口名称造成的异常报错问题
windows·c#·wpf
Rose 使者14 小时前
全球IP归属地查询接口如何用C#进行调用?
c#·api·ip地址
~plus~16 小时前
Harmony核心:动态方法修补与.NET游戏Mod开发
开发语言·jvm·经验分享·后端·程序人生·c#
津津有味道16 小时前
VB.net复制Ntag213卡写入UID
.net·nfc·ntag213·写uid
lv_fu16 小时前
调用.net DLL让CANoe自动识别串口号
.net·dll·capl·canoe·vid pid·自动串口