ch2_03.cs

来自「《c#技术内幕代码》」· CS 代码 · 共 51 行

CS
51
字号
using System;

class TestConstructor
{
   int[] anArray;

   // This is the default constructor (no arguments)   
   public TestConstructor()
   {
      anArray = new int[6];
      for( int i=0; i<6; ++i )
         anArray[i] = i;
   }
      
   
   // This is the "normal" constructor for the class
   public TestConstructor(int arraySize)
   {
      anArray = new int[arraySize];
      for( int i=0; i<arraySize; ++i )
         anArray[i] = i;
   }
   public int getElement(int index)
   {
      if ( index < 0 || index > anArray.Length-1 )
         return -1;
      return anArray[ index ];
   }
}

class CH2_3  {
   public static void Main() 
   {
      // Create an object with 10 elements
      TestConstructor tc = new TestConstructor(10);
      
      // print out every other element to test
      for ( int i=0; i<12; i+=2)
          Console.WriteLine("Element {0} = {1}", i, tc.getElement(i));
	  
      // Create an object with no set # of arguments
      TestConstructor tc1 = new TestConstructor();
      
      // print out every other element to test
      for ( int i=0; i<12; i+=2)
          Console.WriteLine("Element {0} = {1}", i, tc1.getElement(i));

   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?