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);
     }
    }

  • 相关阅读:
    hdu 5795 A Simple Nim 博弈sg函数
    hdu 5724 Chess 博弈sg+状态压缩
    hdu 3094 A tree game 树上sg
    2017"百度之星"程序设计大赛
    hdu 6134 Battlestation Operational 莫比乌斯反演
    HDU 6143 Killer Names DP+快速密
    HDU 6107 Typesetting 倍增
    HDU 6096 String 排序 + 线段树 + 扫描线
    HDU 6086 Rikka with String AC自动机 + DP
    HDU 6073 Matching In Multiplication dfs遍历环 + 拓扑
  • 原文地址:https://www.cnblogs.com/greencolor/p/1716651.html
Copyright © 2011-2022 走看看