zoukankan      html  css  js  c++  java
  • params ref out

    params:  no need initialization,  use variables as parameters

     class App
        {
            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:   initialization variables before entrance of the function

    out:  initialization  variables after out of function 

    class TestApp
    {
     static void outTest(out int x, out int y)
     {//离开这个函数前,必须对x和y赋值,否则会报错。
      //y = x;
      //上面这行会报错,因为使用了out后,x和y都清空了,需要重新赋值,即使调用函数前赋过值也不行
      x = 1;
      y = 2;
     }
     static void refTest(ref int x, ref int y)
     {
      x = 1;
      y = x;
     }
     public static void Main()
     {
      //out test
      int a,b;
      //out使用前,变量可以不赋值
      outTest(out a, out b);
      Console.WriteLine("a={0};b={1}",a,b);
      int c=11,d=22;
      outTest(out c, out d);
      Console.WriteLine("c={0};d={1}",c,d);

      //ref test
      int m,n;
      //refTest(ref m, ref n);
      //上面这行会出错,ref使用前,变量必须赋值

      int o=11,p=22;
      refTest(ref o, ref p);
      Console.WriteLine("o={0};p={1}",o,p);
     }
    }

  • 相关阅读:
    CentOS 7 安装Python pip
    关于 HIVE Beeline 问题
    HIVE 简单总结
    值得一提:关于 HDFS 的 file size 和 block size
    Hadoop作业优化
    一篇文章,掌握所有开源数据库的现状
    dfs.replication 参数 动态修改
    Hadoop YARN ERROR 1/1 local-dirs are bad *, 1/1 log-dirs are bad *
    Hadoop 中关于 map,reduce 数量设置
    CentOS ulimit
  • 原文地址:https://www.cnblogs.com/greencolor/p/1716651.html
Copyright © 2011-2022 走看看