ex-09-07

来自「Programming Csharp Source Code(代码) Prog」· 代码 · 共 72 行

TXT
72
字号
// Example 09-07: Converting arrays

namespace Programming_CSharp
{
   using System;

   // create an object we can
   // store in the array
   public class Employee
   {
      // a simple class to store in the array
      public Employee(int empID)
      {
         this.empID = empID;
      }
      public override  string ToString()
      {
         return empID.ToString();
      }
      private int empID;

      private int size;
   }
  
   public class Tester
   {
      // this method takes an array of objects
      // we'll pass in an array of Employees
      // and then an array of strings
      // the conversion is implicit since both Employee
      // and string derive (ultimately) from object
      public static void PrintArray(object[] theArray)
      {
         Console.WriteLine("Contents of the Array {0}",
            theArray.ToString());

         // walk through the array and print
         // the values. 
         foreach (object obj in theArray)
         {
            Console.WriteLine("Value: {0}", obj);
         }
      }
               
      static void Main()
      {
         // make an array of Employee objects
         Employee[] myEmployeeArray = new Employee[3];

         // initialize each Employee's vaue
         for (int i = 0;i < 3;i++)
         {
            myEmployeeArray[i] = new Employee(i+5);
         }

         // display the values
         PrintArray(myEmployeeArray);

         // create an array of two strings
         string[] array = 
            {
               "hello", "world"
            };
           
         // print the value of the strings
         PrintArray(array);
      }
   }
}


⌨️ 快捷键说明

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