1、添加同一对象
using System.Collections.Generic;
public class Test(){
User u1 = new User ();
u1.Id = 101;
u1.Name = "张三";
User u2 = new User();
u2.Id = 102;
u2.Name = "李四";
ArrayList list = new ArrayList();
list.Add(u1);
list.Add(u2);
foreach(User u in list)
{
Debug.Log(u.Id + "\t" + u.Name);
}
}
2、添加多个不同对象
ArrayList list = new ArrayList();
User u=new User();
u1.Id = 101;
u1.Name = "张三";
Student s=new Student();
s.Id = 101;
s.Name = "李四";
list.Add(u);
list.Add(s);
foreach (object obj in list)
{
if (obj is Student)
{
Debug.Log("AAA");
}
if (obj is User)
{
Debug.Log("BBB");
}
}
3、模板:template
class Chinese
{
string name;
int age;
}
class American
{
string name;
int age;
}
class Person<T>
{
T name;
T age;
}
class Test
{
static void Main(string[] args)
{
Person<Chinese> chinese = new Person<Chinese>();
Person<American> american = new Person<American>();
}
}
4、泛型
泛型是程序设计语言的一种特性,允许程序员在强类型程序设计语言中编写代码时定义一些可变部分,那些部分在使用前必须作出指明。
List<string> list = new List<string>();
list.Add("张三");
list.Add("李四");
list.Add("王五");
list.Add("田六");
list.Add("赵七");
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("for循环:" + list[i]);
}
list.RemoveAt(0);
foreach (string item in list)
{
Console.WriteLine("foreach迭代:" + item);
}