zoukankan      html  css  js  c++  java
  • C# 中方法参数中params ,ref,out 方法的使用

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace RefAndOutAndParams
    {
        class Program
        {
            /// <summary>
            /// 一个方法中可以动态传递多个参数的方法
            /// </summary>
            /// <param name="list"></param>
            public static void MyTestParams(params Object [] list){
    
                for (int i = 0; i < list.Length; i++) {
                    Console.WriteLine(list[i]);
                }
            
            }
            /// <summary>
            /// 单个Ref的方法,方法中Ref传递的参数,在传递进方法之前,必须要有初始值,Ref相当于是一个变量的引用
            /// </summary>
            /// <param name="temp"></param>
            public static void MyTestRef(ref int temp) {
    
                temp = temp + 100;
            
            }
            /// <summary>
            /// 无Ref的方法,
            /// </summary>
            /// <param name="temp"></param>
            public static void NoRef(int temp) {
                temp = temp + 100;
            }
            public static void NoRefStr(string str) {
    
                str = "Tomfanxiaojun";
            }
            /// <summary>
            /// 多个Ref参数
            /// </summary>
            /// <param name="temp1"></param>
            /// <param name="temp2"></param>
            public static void MyTestRefs(ref int temp1, ref int temp2) {
                temp1 = temp1 + 1;
                temp2 = temp2 * 2;  
            }
            /// <summary>
            /// 单个的out参数的方法,该参数在传递这个方法之前,只要定义,不要赋初始值
            /// </summary>
            /// <param name="temp1"></param>
            /// <param name="a"></param>
            public static void MyTestOut(out int temp1,int a) {          
                temp1 = 9+a;  
            }
            public static void MyTestOuts(out int temp1,out int temp2, int a)
            {
                temp1 = 9 + a;
                temp2 = a * 9;
            }
            static void Main(string[] args)
            {
                //动态参数方法的调用
                MyTestParams("OK1", "OK2", 1);
                int a = 200;
                //正常方法的调用
                NoRef(a);
                Console.WriteLine(a);
                //单个Ref参数方法的调用
                MyTestRef(ref a);
                Console.WriteLine(a);
                MyTestRef(ref a);
                Console.WriteLine(a);
    
                int temp1 = 10;
                int temp2 = 20;
                MyTestRefs(ref temp1,ref temp2);
                Console.WriteLine(temp1);
                Console.WriteLine(temp2);
    
                int outvalue;
                MyTestOut(out outvalue, 12);
                Console.WriteLine(outvalue);
                int outvalue1,outvalue2;
                MyTestOuts(out outvalue1,out  outvalue2, 33);
                Console.WriteLine(outvalue1);
                Console.WriteLine(outvalue2);
                string str = "Jake";
                NoRefStr(str);
                Console.WriteLine(str);
                Console.ReadLine();
            }
        }
    }
  • 相关阅读:
    Elementary Methods in Number Theory Exercise 1.5.2
    Elementary methods in number theory exercise 1.5.1 暨 重启C++之路:列出1到210的所有素数
    Elementary Methods in Number Theory Exercise 1.5.5
    《Elementary Methods in Number Theory》勘误
    Elementary Methods in Number Theory Exercise 1.5.2
    Elementary Methods in Number Theory Exercise 1.5.5
    Linux_我理解的逻辑地址、线性地址、物理地址和虚拟地址(补充完整了)
    寄存器和常用汇编指令
    Linux_AMD体系结构学习(内存模型)
    计算机是如何启动的?
  • 原文地址:https://www.cnblogs.com/fanxiaojun/p/2548569.html
Copyright © 2011-2022 走看看