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

  • 相关阅读:
    一款jquery写出来的tab切换
    mouseenter 事件,固定右侧客服特效
    一款兼容pc 移动端的tab切换
    EhCache缓存
    HTML5中与页面显示相关的API
    JAVA获取客户端IP地址
    Oracle11g导出空表
    css兼容问题集合
    使用Java修改Windows注册表
    常用的SQL分页
  • 原文地址:https://www.cnblogs.com/greencolor/p/1716651.html
Copyright © 2011-2022 走看看