using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace ConsoleApplication43 { class ren { private readonly string name; public string Name { get { return name; } } private readonly int age; public int Age { get { return age; } } public ren(string newname, int newage) { name = newname; age = newage; } } class Program { static void Main(string[] args) { List<ren> mylist = new List<ren>(); mylist.Add(new ren("名字1", 15)); mylist.Add(new ren("名字3", 56)); mylist.Add(new ren("名字5", 47)); Console.WriteLine("第2个人是 {0} ,{1}岁。", mylist[1].Name,mylist[1].Age); Console.ReadKey(); } } }
using System; using System.Collections.Generic;class GenericListDemo { static void Main(string[] args) { List<int> alInt32 = new List<int>(); foreach (int number in new int[] { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }) { alInt32.Add(number); } OutputMethod(alInt32); List<double> alDouble = new List<double>(); foreach (double number in new double[] { 10.1, 9.2, 8.3, 7.4, 6.5, 5.6, 4.7, 3.8, 2.9, 1.1 }) { alDouble.Add(number); } OutputMethod(alDouble); } private static void OutputMethod<T>(List<T> al) { foreach (T temp in al) { Console.Write(temp + " "); } Console.WriteLine(); }
List<int> lt2=new List<int>();
lt1.add("Hello ");
lt1.add("World!");
lt2.add(1);
lt2.add(2);
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication43
{
class ren
{
private readonly string name; public string Name
{
get { return name; }
} private readonly int age; public int Age
{
get { return age; }
}
public ren(string newname, int newage)
{
name = newname;
age = newage;
} }
class Program
{
static void Main(string[] args)
{
List<ren> mylist = new List<ren>();
mylist.Add(new ren("名字1", 15));
mylist.Add(new ren("名字3", 56));
mylist.Add(new ren("名字5", 47));
Console.WriteLine("第2个人是 {0} ,{1}岁。", mylist[1].Name,mylist[1].Age);
Console.ReadKey();
}
}
}
using System.Collections.Generic;class GenericListDemo
{
static void Main(string[] args)
{
List<int> alInt32 = new List<int>(); foreach (int number in new int[] { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 })
{
alInt32.Add(number);
}
OutputMethod(alInt32); List<double> alDouble = new List<double>(); foreach (double number in new double[] { 10.1, 9.2, 8.3, 7.4, 6.5, 5.6, 4.7, 3.8, 2.9, 1.1 })
{
alDouble.Add(number);
}
OutputMethod(alDouble);
} private static void OutputMethod<T>(List<T> al)
{
foreach (T temp in al)
{
Console.Write(temp + " ");
}
Console.WriteLine();
}
}