zoukankan      html  css  js  c++  java
  • C# 参考之方法参数关键字:params、ref及out

    C# 参考之方法参数关键字:params、ref及out

    (2009-12-27 18:10:26)

    如果在为方法声明参数时未使用 ref 或 out,则该参数可以具有关联的值。可以在方法中更改该值,但当控制传递回调用过程时,不会保留更改的值。通过使用方法参数关键字,可以更改这种行为。

    params
    params 关键字可以指定在参数数目可变处采用参数的方法参数。

    在方法声明中的 params 关键字之后不允许任何其他参数,并且在方法声明中只允许一个 params 关键字。
    示例:

    view plaincopy to clipboardprint?
    class run  
    {  
        public static void UseParams(params object[] list)  
        {  
            for (int i = 0; i < list.Length; i++)  
            {  
                Console.WriteLine(list[i]);  
            }  
        }  
     
        static void Main()  
        {  
            // 一般做法是先构造一个对象数组,然后将此数组作为方法的参数  
            object[] arr = new object[3] { 100, 'a', "keywords" };  
            UseParams(arr);  
     
            // 而使用了params修饰方法参数后,我们可以直接使用一组对象作为参数  
            // 当然这组参数需要符合调用的方法对参数的要求  
            UseParams(100, 'a', "keywords");  
     
            Console.Read();  
        }  

    class run
    {
        public static void UseParams(params object[] list)
        {
            for (int i = 0; i < list.Length; i++)
            {
                Console.WriteLine(list[i]);
            }
        }

        static void Main()
        {
            // 一般做法是先构造一个对象数组,然后将此数组作为方法的参数
            object[] arr = new object[3] { 100, 'a', "keywords" };
            UseParams(arr);

            // 而使用了params修饰方法参数后,我们可以直接使用一组对象作为参数
            // 当然这组参数需要符合调用的方法对参数的要求
            UseParams(100, 'a', "keywords");

            Console.Read();
        }
    }

    ref
    ref 关键字使参数按引用传递。其效果是,当控制权传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。

    若要使用 ref 参数,则方法定义和调用方法都必须显式使用 ref 关键字。
    传递到 ref 参数的参数必须最先初始化。这与 out 不同,out 的参数在传递之前不需要显式初始化。
    属性不是变量,因此不能作为 ref 参数传递。
    尽管 ref 和 out 在运行时的处理方式不同,但它们在编译时的处理方式是相同的。因此,如果一个方法采用 ref 参数,而另一个方法采用 out 参数,则无法重载这两个方法。例如,从编译的角度来看,以下代码中的两个方法是完全相同的。如果尝试这么做,将导致不能编译该代码。
    如果一个方法采用 ref 或 out 参数,而另一个方法不采用这两类参数,则可以进行重载。
    示例:

    view plaincopy to clipboardprint?
    class Run  
    {  
        public static void UseRef(ref int i)  
        {  
            i += 100;  
            Console.WriteLine("i = {0}", i);  
        }  
     
        static void Main()  
        {  
            int i = 10;  
     
            // 查看调用方法之前的值  
            Console.WriteLine("Before the method calling: i = {0}", i);  
     
            UseRef(ref i);  
     
            // 查看调用方法之后的值  
            Console.WriteLine("After the method calling: i = {0}", i);  
            Console.Read();  
        }  
    }  
     
     
    class Run
    {
        public static void UseRef(ref int i)
        {
            i += 100;
            Console.WriteLine("i = {0}", i);
        }

        static void Main()
        {
            int i = 10;

            // 查看调用方法之前的值
            Console.WriteLine("Before the method calling: i = {0}", i);

            UseRef(ref i);

            // 查看调用方法之后的值
            Console.WriteLine("After the method calling: i = {0}", i);
            Console.Read();
        }
    }

    out
    out 关键字会导致参数通过引用来传递。这与 ref 关键字类似。

    与 ref 的不同之处:

    ref 要求变量必须在传递之前进行初始化。
    尽管作为 out 参数传递的变量不需要在传递之前进行初始化,但需要调用方法以便在方法返回之前赋值。
    示例:

    view plaincopy to clipboardprint?
    class Run  
    {  
        public static void UseRef(ref int i)  
        {  
            i += 100;  
            Console.WriteLine("i = {0}", i);  
        }  
     
        static void Main()  
        {  
            int i ;  
     
            // 查看调用方法之前的值  
            Console.WriteLine("Before the method calling: i = {0}", i);  
     
            UseRef(ref i);  
     
            // 查看调用方法之后的值  
            Console.WriteLine("After the method calling: i = {0}", i);  
            Console.Read();  
        }  
    }  
     
     

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/teg2007/archive/2009/04/03/4013700.aspx#

  • 相关阅读:
    SQL server中自定义排序
    安装nodejs版本模块报错notsup Unsupported platform for n
    vue项目中一些标签直接放在<template>下会报错Failed to compile with 1 errors
    vue中使用element-ui出现Couldn't find preset "es2015" relative to directory
    解决两个相邻的span,或者input和button中间有间隙,在css中还看不到
    VsCode中代码折叠快捷键
    npm 操作代码
    vue项目打包成html,在本地点击直接能打开
    地图只显示部分区域,其他地区不显示
    vs里颜色显示块怎样显示
  • 原文地址:https://www.cnblogs.com/tabcdt/p/2916734.html
Copyright © 2011-2022 走看看