zoukankan      html  css  js  c++  java
  • C#中引用参数ref和输出参数out

    引用参数 用于按引用传递自变量。 为引用参数传递的自变量必须是具有明确值的变量,并且在方法执行期间,引用参数指明的存储位置与自变量相同。 引用参数使用 ref 修饰符进行声明。 

    输出参数 用于按引用传递自变量。 输出参数与引用参数类似,不同之处在于,不要求向调用方提供的自变量显式赋值。 输出参数使用 out 修饰符进行声明。 下面分别示例展示了如何使用 ref out  关键字

    using System;
    
    class RefExample
    {
        static void Swap(ref int x, ref int y)
        {
            int temp;
            temp = x;
            x = y;
            y = temp;
    
        }
        public static void SwapExample()
        {
            int i = 1, j = 2;
            Swap(ref i,ref j);
            Console.WriteLine($"{i}{j}");
        }
    
        /*static void Main(string[] args)
        {
            SwapExample();
        }
        */
    }
    class OutExample
    {
        static void Divide(int x, int y,out int result,out int remainder)
        {
            result = x / y;
            remainder = x % y;
    
        }
    
        public static void OutUsage()
        {
            Divide(10, 3, out int res, out int rem);
            Console.WriteLine("{0} {1}", res, rem);
        }
    
        static void Main()
        {
            OutUsage();
            RefExample.SwapExample();
            
            
    
        }
    }
  • 相关阅读:
    软件工程概论课后作业2
    第三周进度表
    软件工程概论课后作业1
    第二周进度表
    9.异常处理
    《构建之法》阅读笔记二
    《构建之法》阅读笔记一
    第五周进度表
    软件工程个人作业03
    第四周进度表
  • 原文地址:https://www.cnblogs.com/Mr-Prince/p/12045702.html
Copyright © 2011-2022 走看看