每天5分钟玩转C#/.NET之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 可能会引入潜在的错误,打破正常的控制流程。

相关推荐
这不小天嘛42 分钟前
JAVA八股——redis篇
java·开发语言·redis
逝水无殇2 小时前
C# 字符串(String)详解
开发语言·后端·c#
精明的身影2 小时前
C++自学之路1:Hello world
开发语言·c++
leo__5203 小时前
基于导航数据的扩展卡尔曼滤波(EKF)MATLAB 实现
开发语言·matlab
gugucoding3 小时前
25. 【C语言】二进制文件与随机读写
c语言·开发语言
北极星日淘3 小时前
中古货品品相评级算法实战|Java权重计分实现标准化五级品相体系
开发语言·python
小巧的砖头3 小时前
C#会重蹈覆辙吗?系列之2:反射及元数据的性能问题
开发语言·前端·c#
凯瑟琳.奥古斯特3 小时前
力扣1009补码解法C++实现
开发语言·c++·算法·leetcode·职场和发展
2601_963645924 小时前
【2026最新】MATLAB教程(附安装包,非常详细)
开发语言·matlab·信息可视化
闲猫5 小时前
Python 虚拟环境 virtualenv & uvicorn 服务搭建 & FAstAPI 使用
开发语言·python