📄 ex-09-07
字号:
// 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -