zoukankan      html  css  js  c++  java
  • C# 方法的参数传递机制

    ▲值参数(Value Parameter)

    方法名称(参数类型 参数名称 [,参数类型 参数名称])

    ▲引用参数(Reference Parameter)

    方法名称(ref 参数类型 参数名称 [,ref 参数类型 参数名称])

    ▲输出参数(Output Parameter)

    方法名称(out参数类型 参数名称 [,out 参数类型 参数名称])

    public static void valueMethod(int i) //值参数
            {
                i++;
            }
    
            public static void referenceMethod(ref int i) //引用参数
            {
                i++;
            }
    
            public static void outputMethod(out int i) //输出参数  
            {
                i = 0;    //ref 或out 参数不能有默认值,out函数必须初始化
                i++;         
            }
    
            static void Main(string[] args)
            {
                int i = 0;
                valueMethod(i);
                Console.WriteLine("i="+i); //结果0
    
                int j = 0;
                referenceMethod(ref j);
                Console.WriteLine("j="+j); //结果1
    
                int k;
                outputMethod(out k);
                Console.WriteLine("k="+k); //结果1
    
                Console.ReadLine();
    
              }

    总结:ref参数和out参数区别在于,引用参数是先初始化后调用方法,输出参数是方法里初始化变量。

  • 相关阅读:
    ColorMask
    InfoPanel
    什么是三消游戏
    Display file information in the document window
    Layer Comps
    Add words to your picture
    为什么质数是无穷的?
    嘉年华的来历
    MonoBehaviour.OnValidate
    Loadrunner中百分比模式和Vuser模式
  • 原文地址:https://www.cnblogs.com/han1982/p/2685025.html
Copyright © 2011-2022 走看看