今天写的一段代码关于泛型的简单使用
时间:2010-08-27 来源:卑鄙De小贝
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace ConsoleApplication1
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 int List_i = 1;
13 foreach (var i in List())
14 {
15 Console.WriteLine("{0}:{1},{2}", List_i, i.Id, i.Name);
16 List_i += 1;
17 }
18 Console.ReadLine();
19 }
20 static public List<User> List()
21 {
22 List<User> list = new List<User>();
23 list.Add(new User ( 1, "a" ));
24 list.Add(new User ( 2, "b" ));
25 return list;
26 }
27 }
28 class User
29 {
30 int id;
31 string name;
32 public int Id
33 {
34 get { return id; }
35 set { id = value; }
36 }
37 public string Name
38 {
39 get { return name;}
40 set { name = value;}
41 }
42 public User(int id,string name)
43 {
44 this.id = id;
45 this.name = name;
46 }
47 }
48 }
49
这是运行结果,希望能帮到和我一样的新人!
相关阅读 更多 +