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();
            
            
    
        }
    }
  • 相关阅读:
    Java ClassLoader
    Java Hashcode
    Java 装箱 拆箱
    Java 深拷贝,浅拷贝
    Java IO流
    JVM内存模型
    C语言中的__FILE__、__LINE__和#line
    OpenMP和MPI的区别
    visual studio代码备份精减
    MVVM设计模式在WPF中的应用
  • 原文地址:https://www.cnblogs.com/Mr-Prince/p/12045702.html
Copyright © 2011-2022 走看看