基本类型--数组
13.1数组的类型转换
13.2所有数组都隐式继承自System.Array
13.3所有数组都隐式实现IEnumerable,ICollection和Ilist
13.4数组的传递与返回
13.5创建下界非0的数组
13.2所有数组都隐式继承自System.Array
13.3所有数组都隐式实现IEnumerable,ICollection和Ilist
13.4数组的传递与返回
13.5创建下界非0的数组
public sealed class DynamicArrays {
public static void Main() {
//创建这样一个数组[20052009][14]
Int32[] lowerBounds ={ 2005, 1 };//第二维大小
Int32[] lengths ={ 5, 4 };//第一维大小
Decimal[,] quarterlyRevenue = (Decimal[,])
Array.CreateInstance(typeof(Decimal),lengths,lowerBounds);
Console.WriteLine("{0,4}{1,9}{2,9}{3,9}{4,9}", "Year", "Q1", "Q2", "Q3", "Q4");
Int32 firstyear = quarterlyRevenue.GetLowerBound(0);//2005
Int32 lastyear = quarterlyRevenue.GetUpperBound(0);//2009
Int32 firstQuarter = quarterlyRevenue.GetLowerBound(1);//1
Int32 lastQuarter = quarterlyRevenue.GetUpperBound(1);//4
for (Int32 y = firstyear; y <= lastyear; y++)
{
Console.WriteLine(y + " ");
for (Int32 q = firstQuarter; q <= lastQuarter; q++)
Console.Write("{0,9:C}", quarterlyRevenue[y, q]);
Console.WriteLine();
}
Console.ReadLine();
}
}
13.6数组访问性能public static void Main() {
//创建这样一个数组[20052009][14]
Int32[] lowerBounds ={ 2005, 1 };//第二维大小
Int32[] lengths ={ 5, 4 };//第一维大小
Decimal[,] quarterlyRevenue = (Decimal[,])
Array.CreateInstance(typeof(Decimal),lengths,lowerBounds);
Console.WriteLine("{0,4}{1,9}{2,9}{3,9}{4,9}", "Year", "Q1", "Q2", "Q3", "Q4");
Int32 firstyear = quarterlyRevenue.GetLowerBound(0);//2005
Int32 lastyear = quarterlyRevenue.GetUpperBound(0);//2009
Int32 firstQuarter = quarterlyRevenue.GetLowerBound(1);//1
Int32 lastQuarter = quarterlyRevenue.GetUpperBound(1);//4
for (Int32 y = firstyear; y <= lastyear; y++)
{
Console.WriteLine(y + " ");
for (Int32 q = firstQuarter; q <= lastQuarter; q++)
Console.Write("{0,9:C}", quarterlyRevenue[y, q]);
Console.WriteLine();
}
Console.ReadLine();
}
}
作者:青羽