,转载C#中泛型学习笔记

 前言:这篇博客我主要说一下C#中泛型的使用,也就是List和Dictionary字典集合的使用,我在这里说的主要说的是如何去使用,而不是长篇大论的去研究泛型的底层,但我们有一定程序的时候在研究,使学习的能够很快的学习集合然后自己研究集合的一些高级用法,不在最后还列举出了一些常用的小案例。

  1. 泛型集合

(1) 泛型集合就是不确定的集合,语法中有一个尖括号,里面放什么类型,这个集合就变成什么类型

(2)List

1)举例说明:

View Code?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

staticvoidMain(string[] args)

{

List<int> listInt =newList<int>();

listInt.AddRange(newint[] { 1, 34, 54, 65, 76, 78 });

intsum = 0;

for(inti = 0; i < listInt.Count; i++)

{

sum += listInt[i];

}

Console.WriteLine(sum);

Console.ReadKey();

}

(3)Dictionary (Dictionary<TKey,TValue>)

定义一个泛型集合:Dictionary<TKey,Tvalue> dic=new Dictionary<TKey,Tvalue>();

1)增加

Add 将指定的键值对添加到字典集合中

方法原型:void dic.Add(T key,T Value)

View Code?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Dictionary<string,string> openWith =newDictionary<string,string>();

try

{

openWith.Add("txt","notepad.exe");

openWith.Add("bmp","paint.exe");

openWith.Add("dib","paint.exe");

openWith.Add("rtf","wordpad.exe");

openWith.Add("txt","winword.exe");

}

catch(ArgumentException)

{

Console.WriteLine("添加失败,请检查");

}

//输出结果是添加失败,请检查,以为添加了相同的键

2)删除

Remove 从字典集合中移除指定的键的值

方法原型:bool dic.Remove(TKey key);

View Code?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

Dictionary<string,string> openWith =newDictionary<string,string>();

openWith.Add("txt","notepad.exe");

openWith.Add("bmp","paint.exe");

openWith.Add("dib","paint.exe");

openWith.Add("rtf","wordpad.exe");

openWith.Remove("txt");

foreach(var iteminopenWith)

{

Console.WriteLine(item.Key);

}

//输出结果:bmp dib rtf

Clear 从字典集合中移除所有的值

方法原型:voiddic.Clear();

Dictionary<string,string> openWith =newDictionary<string,string>();

openWith.Add("txt","notepad.exe");

openWith.Add("bmp","paint.exe");

openWith.Add("dib","paint.exe");

openWith.Add("rtf","wordpad.exe");

openWith.Clear();

foreach(var iteminopenWith)

{

Console.WriteLine(item.Key);

}

//输出结果为空

3)查询

ContainsKey 得到字典集合中是否包含指定的键

方法原型:bool dic.ContainsKey(TKey,key);

View Code?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Dictionary<string,string> openWith =newDictionary<string,string>();

openWith.Add("txt","notepad.exe");

openWith.Add("bmp","paint.exe");

openWith.Add("dib","paint.exe");

openWith.Add("rtf","wordpad.exe");

if(!openWith.ContainsKey("txt"))

{

openWith.Add("txt","notepat++");

}

else

{

Console.WriteLine("已经存在");

}

//输出结果:已经存在

COntainsValue 得到字典集合中是否包含指定的值

方法原型:bool dic.ContainsValue(TValue,value);

Dictionary<string, string> openWith = new Dictionary<string, string>();

openWith.Add("txt", "notepad.exe");

openWith.Add("bmp", "paint.exe");

openWith.Add("dib", "paint.exe");

openWith.Add("rtf", "wordpad.exe");

if (openWith.ContainsValue("paint.exe"))

{

Console.WriteLine("已经存在");

}

//输出结果:已经存在

4)TryGetValue 获得于指定的键相关联的值

方法原型:bool dic.TryGetValue(TKey key,out TVlaue value);

View Code?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

Dictionary<string,string> openWith =newDictionary<string,string>();

openWith.Add("txt","notepad.exe");

openWith.Add("bmp","paint.exe");

openWith.Add("dib","paint.exe");

openWith.Add("rtf","wordpad.exe");

stringvalue ="";

if(openWith.TryGetValue("rtf",outvalue))

{

Console.WriteLine("Key=rtf,value={0}", value);

}

else

{

Console.WriteLine("根据rtf键没有找到对应的值");

}

//输出结果:key=rtf,value=wordpad.exe

1)举例说明:

static void Main(string[] args)

{

Dictionary<char, string> dic = new Dictionary<char, string>();

dic.Add('1', "爱情这东西");

foreach (KeyValuePair<char, string> item in dic)

{

Console.WriteLine(item);

}

Console.ReadKey();

}

(4)案例1:把分拣奇数的程序用泛型实现

View Code?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

staticvoidMain(string[] args)

{

stringstr ="3 45 65 34 68 67 87 98";

//1 split

string[] nums = str.Split(newchar[] {' '}, StringSplitOptions.RemoveEmptyEntries);

//2 list<string>

List<string> odd =newList<string>();//奇数

List<string> even =newList<string>();//偶数

//3 for循环判断奇偶

for(inti = 0; i < nums.Length; i++)

{

//第一种方法

//int num = Convert.ToInt32(nums[i]);

//if (num % 2 == 0)

//{

// even.Add(nums[i]);

//}

//else

//{

// odd.Add(nums[i]);

//}

//第二种方法

stringnum = nums[i];

charch = num[num.Length - 1];

intlast = ch -'0';

if((nums[i][nums[i].Length - 1] -'0') % 2 == 0)

{

even.Add(nums[i]);

}

else

{

odd.Add(nums[i]);

}

}

odd.AddRange(even);

//4转换

Console.WriteLine(string.Join(" ", odd.ToArray()));

}

(5)案例2:将int数组中的奇数放到一个新的int数组中返回

View Code?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

staticvoidMain(string[] args)

{

int[] nums = { 1, 3, 5, 565, 76, 78, 98, 90, 4, 545 };

List<int> listInt =newList<int>();

for(inti = 0; i < nums.Length; i++)

{

if(nums[i] % 2 == 1)

{

listInt.Add(nums[i]);

}

}

for(inti = 0; i < listInt.Count; i++)

{

Console.WriteLine(listInt[i] +" ");

}

Console.ReadKey();

}

(6)案例3:从一个整数的List<int>中取出最大数

View Code?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

staticvoidMain(string[] args)

{

int[] nums = { 2, 34, 454, 65, 76, 77, 778, 898, 989 };

intmax =int.MinValue;

intmin =int.MaxValue;

List<int> listInt =newList<int>();

listInt.AddRange(nums);

for(inti = 0; i < listInt.Count; i++)

{

if(min > listInt[i])

{

min = listInt[i];

}

if(max < listInt[i])

{

max = listInt[i];

}

}

Console.WriteLine(max);

Console.WriteLine(min);

}

(7)把123转换为"壹贰叁"

View Code?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

staticvoidMain(string[] args)

{

stringvar ="壹贰叁肆伍陆柒捌玖";

Dictionary<char,char> dic =newDictionary<char,char>();

for(inti = 0; i <var.Length ; i++)

{

dic.Add((char)(i +'0'), var[i]);

}

while(true)

{

Console.Write("请输入一行数字:");

stringstr = Console.ReadLine();

StringBuilder sb =newStringBuilder();

for(inti = 0; i < str.Length; i++)

{

charnum = str[i];

charword = dic[num];

sb.Append(word);

}

Console.WriteLine(sb.ToString());

Console.ReadKey();

}

}

(8)计算字符串中每种字符出现的次数

View Code?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

staticvoidMain(string[] args)

{

Dictionary<char,int> dic =newDictionary<char,int>();

Console.Write("请输入一句话");

stringstr = Console.ReadLine();

for(inti = 0; i < str.Length; i++)

{

//dic.Add(str[i], 1);

//dic[str[i]]++;

charcurrent = str[i];

if(dic.ContainsKey(current))

{

//如果集合不存在这个数据

//dic[current] += 1;

dic[current]++;

}

else

{

//如果集合中不存在这个数据

dic.Add(current, 1);

}

}

foreach(KeyValuePair<char,int> itemindic)

{

Console.WriteLine("子符{0}出现了{1}次", item.Key, item.Value);

}

Console.ReadKey();

}

  1. Dictionary就是Hashtable的泛型形式

(1) 哈尔算法是一个函数

Add(Key,Value);

dic[Key];

(2)哈希算法是一个通过Key来计算地址的函数

1)传入一个key和一个value后

2)通过哈希算法计算key的到一个地址

3)将地址存入键值对集合,并将value存入地址所在的地方

4)等到访问的时候直接通过key计算出地址,直接找到存储的变量

  1. 能不能用for循环遍历一个集合Dic

(1) 在for循环中如果不使用对应的递增序号,"我"就认为不叫使用了for循环

(2)foreach循环的过程

1)找到数据源,调用GetEnumertor方法,得到枚举值

2)in,调用MoveNext方法

3)如果MoveNext返回true,使用Current得到当前数据

4)如果返回false,则跳出循环

static void Main(string[] args)

{

Dictionary<string, string> dic = new Dictionary<string, string>();

dic.Add("1111", "2222");

dic.Add("0000", "3333");

var enumrator = dic.GetEnumerator();

//while (enumrator.MoveNext())

//{

// Console.WriteLine(enumrator.Current.Key + "," + enumrator.Current.Value);

//}

for (; enumrator.MoveNext(); )

{

Console.WriteLine(enumrator.Current.Key + "," + enumrator.Current.Value);

}

}

  1. 等于

(1) Equals 确定指定的Object是否等于当前的Object类型

方法原型:

bool Equals(Object obj)

Object Obj1 = new Object();

Object Obj2 = new Object();

Console.WriteLine(Obj1.Equals(Obj2));

Obj2 = Obj1;

Console.WriteLine(Obj1.Equals(Obj2));

输出结果: False,True