⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch2_03.cs

📁 《c#技术内幕代码》
💻 CS
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -