• C# 经典:ref 和 out 的区别详解


    在C#中,ref和out关键字用于按引用传递变量,它们在变量传递、输出参数、返回值以及异常处理等方面有一些重要区别。本文将详细阐述这些差异。

    1. 变量传递

    ref和out关键字都可以用于方法的参数传递。它们的主要区别在于如何处理变量的引用。

    • ref关键字在方法调用时创建了一个引用,该引用指向调用方法时传递的变量。在方法内部,可以通过引用修改变量的值,并且这些更改将反映在原始变量上。
    void ModifyValue(ref int a)
    {
        a = 5;
    }
    class Program
    {
        static void Main(string[] args)
        {
            int b = 1;
            ModifyValue(ref b);
            Console.WriteLine(b); // 输出:5
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • out关键字也在方法调用时创建了一个引用,但是与ref不同,out参数在方法内部不需要初始化。然而,out参数必须在方法返回之前被赋值。
    void SetValue(out int a)
    {
        a = 5;
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            int b;
            SetValue(out b);
            Console.WriteLine(b); // 输出:5
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2. 输出参数

    ref和out关键字都可以用作输出参数,但是它们在输出参数的使用上有一些差异。

    • ref输出参数通常用于需要修改输入参数的情况。在方法内部,可以通过引用直接修改输入参数的值。
    ref int AddAndReturn(int a, int b)
    {
        return a + b;
    }
    class Program
    {
        static void Main(string[] args)
        {
            int c = AddAndReturn(1, 2);
            Console.WriteLine(c); // 输出:3
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • out输出参数通常用于方法需要计算多个输出值的情况。在方法内部,可以通过out参数返回一个或多个值。
    out int MultiplyAndReturn(int a, int b)
    {
        return a * b;
    }
    class Program
    {
        static void Main(string[] args)
        {
            int c, d;
            MultiplyAndReturn(1, 2, out c, out d);
            Console.WriteLine(c); // 输出:2
            Console.WriteLine(d); // 输出:2
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    3. 返回值

    ref和out关键字都可以用于方法的返回值,但是它们在返回值的使用上有一些差异。

    • ref返回值通常用于需要返回多个值的方法。在方法内部,可以通过引用返回一个或多个值。
    ref int GetValues()
    {
        int a = 1;
        int b = 2;
        return ref a;
    }
    class Program
    {
        static void Main(string[] args)
        {
            int c = GetValues();
            Console.WriteLine(c); // 输出:1
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • out返回值通常用于需要返回单个值的方法。在方法内部,可以通过out参数返回一个值。
    out int GetValue()
    {
        int a = 1;
        return a;
    }
    class Program
    {
        static void Main(string[] args)
        {
            int c = GetValue();
            Console.WriteLine(c); // 输出:1
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    4. 异常处理

    ref和out关键字在异常处理方面也有一些差异。

    • ref参数在方法内部发生异常时,异常会被抛出,并且原始变量的值不会被修改。
    void ModifyValue(ref int a)
    {
        try
        {
            // 模拟发生异常
            throw new Exception("An error occurred");
        }
        catch (Exception)
        {
            a = 0;
            throw;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int b = 1;
                ModifyValue(ref b);
            }
            catch (Exception)
            {
                Console.WriteLine("Exception occurred.");
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • out参数在方法内部发生异常时,异常会被抛出,但是out参数的值不会被设置。
    void SetValue(out int a)
    {
        try
        {
            // 模拟发生异常
            throw new Exception("An error occurred");
        }
        catch (Exception)
        {
            a = 0;
            throw;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int b;
                SetValue(out b);
            }
            catch (Exception)
            {
                Console.WriteLine("Exception occurred.");
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    总结:

    ref和out在C#中都是用于按引用传递变量的关键字。它们在变量传递、输出参数、返回值以及异常处理等方面有一些重要区别。ref关键字在方法调用时创建了一个引用,该引用指向调用方法时传递的变量。在方法内部,可以通过引用修改变量的值,并且这些更改将反映在原始变量上。out关键字也在方法调用时创建了一个引用,但是与ref不同,out参数在方法内部不需要初始化。然而,out参数必须在方法返回之前被赋值。在输出参数和返回值方面,ref和out都可以用于返回多个值或单个值。然而,它们在方法内部发生异常时的处理方式有所不同。ref参数在异常发生时,原始变量的值不会被修改,而out参数的值不会被设置。

  • 相关阅读:
    xss.haozi.me靶场练习
    JS,DOM试题1,在实践中应用,非常详细!!
    人体关键点识别易语言代码
    HackTheBox Ambassador 枚举获得用户shell,git consul API提权
    SpringBoot保姆级教程(七)Thymeleaf
    javascript 原生操作子 cookie 的工具类
    Spring Security OAuth2实现单点登录:简化多个系统之间的登录流程
    Lambda表达式和Stream API (尚硅谷)
    网盘资源搜索引擎大全,找资源不再难
    【0104】查找PostgreSQL数据库和表的大小
  • 原文地址:https://blog.csdn.net/qq_35320456/article/details/136293402