using System;
using System.Collections.Generic;
using System.Collections;
public class A
{
static int Count = 0;
int curNum;
public A()
{
curNum = ++ Count;
}
public void print()
{
Console.WriteLine("Hello World ! " + curNum);
}
}
public class GetNameTest
{
public static void Main()
{
ArrayList arr = new ArrayList(10);
arr.Add(1);
arr.Add("Fuck");
arr.Add('U');
arr.Add(new A());
for (int i = 0; i < arr.Count; i++)
Console.WriteLine(arr[i]);
A cl = (A)arr[3];
cl.print();
Array ar = Array.CreateInstance(typeof(string),10);
for (int i = 0; i < ar.Length;i ++ )
ar.SetValue("Hello " + i.ToString(),i);
foreach (string s in ar)
Console.WriteLine(s);
Array ar1 = Array.CreateInstance(typeof(A), 10);
for (int i = 0; i < ar1.Length; i++)
ar1.SetValue(new A(), i);
foreach (A s in ar1)
s.print();
Array ar2 = Array.CreateInstance(typeof(string), 10);
//ar2.SetValue(new A(),0);
}
}
Array固定大小,使用Array.CreateInstance创建实例,并指定类型和大小;
ArrayList 大小动态,类型任意,速度慢些~
Array && ArrayList C#
最新推荐文章于 2024-09-05 22:55:24 发布