C# : ref out params and override

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace test3
  5. {
  6.     abstract class ShapesClass
  7.     {
  8.         abstract public int Area();
  9.     }
  10.     class Square : ShapesClass
  11.     {
  12.         int x, y;
  13.         // Because ShapesClass.Area is abstract, failing to override
  14.         // the Area method would result in a compilation error.
  15.         public Square(int i , int j)
  16.         {
  17.             x = i;
  18.             y = j;
  19.         }
  20.         public override int Area()
  21.         {
  22.             return x * y;
  23.         }
  24.     }
  25.     /*
  26.          要扩展或修改继承的方法、属性、索引器或事件的抽象实现或虚实现,必须使用 override 修饰符。
  27.     */
  28.     class Program
  29.     {
  30.         public static void useparams(params int[] list)
  31.         {
  32.             for (int i = 0; i < list.Length; i++)
  33.             {
  34.                 Console.WriteLine(list[i]);
  35.             }
  36.             Console.WriteLine();
  37.          // params 关键字 用于方法参数列表长度不定的情况;
  38.         }
  39.         static void useref(ref int i)
  40.         {
  41.             i = 44;
  42.         //ref 关键字表示使用引用类型参数。其效果是,在方法中对参数所作的任何更改都将反映在改变量中。
  43.         //   方法的定义和调用都必须显示的使用ref关键字。
  44.         }
  45.         static void useout(out int i)
  46.         {
  47.             i = 45;
  48.         //out 关键字 表示使用引用类型参数。和ref的效果是一样的,不同之处在于,ref要求变量在引用之间必须初始化,
  49.         // 而out无需进行初始化。
  50.         }
  51.         /*
  52.          尽管ref和out 关键字在运行时的处理方式不同,但是在编译时的处理方式是一样的,因此,如果一个
  53.          * 方法是用ref 参数,另一个方法是用out参数 则无法重载这两个方法。
  54.          * 例如:
  55.          * class cs0663_example
  56.          * {
  57.          *   public void samplemethod(out int i ){}
  58.          *   public void samplemethod(ref int i ){}
  59.          *   //这两个方法是一样的不可以这样重载 
  60.          * }
  61.          */
  62.         static void Main(string[] args)
  63.         {
  64.             Console.WriteLine("*************************  params  *******************************");
  65.             useparams(1, 2, 3, 4);
  66.             int[] myarray = new int[3] { 10, 11, 12 };
  67.             useparams(myarray);
  68.             Console.WriteLine("************************* ref  *******************************");
  69.             int val = 1;
  70.             Console.WriteLine("the val is :" + val);
  71.             useref(ref val);
  72.             Console.WriteLine("the val is :" + val);
  73.             Console.WriteLine("************************  out  ******************************");
  74.             int val2;
  75.             useout(out val2);
  76.             Console.WriteLine("the val2 is :" + val2);
  77.             Console.WriteLine("************************ override ******************************");
  78.             Square s = new Square(12,23);
  79.             Console.WriteLine(s.Area().ToString());
  80.         }
  81.     }
  82. }

你可能感兴趣的:(C# : ref out params and override)