C#语言入门详解-030泛型、partial类、枚举、结构

  1. 泛型(generic)无处不在
  • 为什么需要泛型:避免成员膨胀或者类型膨胀

  • 正交性:泛型类型(类/接口/委托/……)、泛型成员(属性/方法/字段/……)

  • 类型方法的参数推断

  • 泛型与委托、lambda表达式

  1. partial类
  • 减少派生的类

  • partial类与Entity Framework

  • partial类与Windows Forms、WPF、ASP.NET Core

  1. 枚举类型
  • 人为限定取值范围的整数

  • 整数值的对应

  • 比特位式用法

  1. 结构体(struct)
  • 值类型,可装/可拆

  • 可实现接口,不能派生自类/结构体

  • 不能有显示无参构造器

泛型类

C#语言入门详解-030泛型、partial类、枚举、结构 - 图1

  1. using System;
  2. namespace _030_泛型_partial_枚举_结构体 {
  3. class Program {
  4. static void Main(string[] args)
  5. {
  6. var apple = new Apple() { Color = "Red" };
  7. var book = new Book() { Name = "New Book" };
  8. Box<Apple> box = new Box<Apple>() { Cargo = apple };
  9. Box<Book> bookBox = new Box<Book>() { Cargo = book };
  10. Console.WriteLine(box.Cargo.Color);
  11. Console.WriteLine(bookBox.Cargo.Name);
  12. Console.ReadKey();
  13. }
  14. }
  15. class Apple {
  16. public string Color { get; set; }
  17. }
  18. class Book {
  19. public string Name { get; set; }
  20. }
  21. class Box<TCargo> {
  22. public TCargo Cargo { get; set; }
  23. }
  24. }

泛型接口

  1. using System;
  2. namespace _030_泛型_partial_枚举_结构体 {
  3. class Program {
  4. static void Main(string[] args)
  5. {
  6. Student<int> stu=new Student<int>();
  7. stu.Id = 101;
  8. stu.Name = "Tim";
  9. Student<ulong> stu1=new Student<ulong>();
  10. stu1.Id = 1000000000001;
  11. stu1.Name = "Jack";
  12. Stu2 stu2=new Stu2();
  13. stu2.Id = 100000000000000;
  14. stu2.Name = "Maomao";
  15. }
  16. }
  17. interface IUnique<TId>
  18. {
  19. TId Id { get; set; }
  20. }
  21. class Student<TId> : IUnique<TId> {
  22. public TId Id { get; set ; }
  23. public string Name { get; set; }
  24. }
  25. class Stu2: IUnique<ulong> {
  26. public ulong Id { get ; set; }
  27. public string Name { get; set; }
  28. }
  29. }

泛型数组

  1. static void Main(string[] args)
  2. {
  3. IList<int> list = new List<int>();
  4. for (int i = 0; i < 100; i++)
  5. {
  6. list.Add(i);
  7. }
  8. foreach (int i in list)
  9. {
  10. Console.WriteLine(i);
  11. }
  12. Console.ReadKey();
  13. }
  1. static void Main(string[] args)
  2. {
  3. IDictionary<int,string> dict=new Dictionary<int, string>();
  4. dict[1] = "Tim";
  5. dict[2] = "Mack";
  6. Console.WriteLine($"student #1 is {dict[1]}");
  7. Console.WriteLine($"student #2 is {dict[2]}");
  8. Console.ReadKey();
  9. }

C#语言入门详解-030泛型、partial类、枚举、结构 - 图2

泛型方法

  1. static void Main(string[] args)
  2. {
  3. int[] a1 = { 1, 2, 3, 4, 5 };
  4. int[] a2 = { 1, 2, 3, 4, 5, 6 };
  5. double[] a3 = { 1.1, 2.2, 3.3, 4.4, 5.5 };
  6. double[] a4 = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 };
  7. var result = Zip(a1, a2);
  8. Console.WriteLine(string.Join(",", result));
  9. var result2 = Zip(a3, a4);
  10. Console.WriteLine(string.Join(",", result2));
  11. Console.ReadKey();
  12. }
  13. private static T[] Zip<T>(T[] a1, T[] a2)
  14. {
  15. T[] zipped = new T[a1.Length + a2.Length];
  16. int ai = 0, bi = 0, zi = 0;
  17. do
  18. {
  19. if (ai < a1.Length) zipped[zi++] = a1[ai++];
  20. if (bi < a2.Length) zipped[zi++] = a2[bi++];
  21. } while (ai < a1.Length | bi < a2.Length);
  22. return zipped;
  23. }

C#语言入门详解-030泛型、partial类、枚举、结构 - 图3

泛型委托

  1. static void Main(string[] args)
  2. {
  3. Action<string> a1 = Say;
  4. a1("Tom");
  5. Action<int> a2 = Mul;
  6. a2(3);
  7. Console.ReadKey();
  8. }
  9. static void Say(string str)
  10. {
  11. Console.WriteLine($"Hello {str}!");
  12. }
  13. static void Mul(int x)
  14. {
  15. Console.WriteLine(x * 100);
  16. }

C#语言入门详解-030泛型、partial类、枚举、结构 - 图4

  1. static void Main(string[] args)
  2. {
  3. Func<int, int, int> func = Add;
  4. var res = func(100, 200);
  5. Console.WriteLine(res);
  6. Func<double, double, double> func2 = Add;
  7. var res2 = func2(23.6, 566.6);
  8. Console.WriteLine(res2);
  9. Console.ReadKey();
  10. }
  11. static int Add(int a, int b)
  12. {
  13. return a + b;
  14. }
  15. static double Add(double a, double b)
  16. {
  17. return a + b;
  18. }

C#语言入门详解-030泛型、partial类、枚举、结构 - 图5

  1. Func<int, int, int> func = (int a, int b) => { return a + b; };//泛型与lamuda表达式配合使用
  2. var res = func(100, 200);
  3. Console.WriteLine(res);
  4. Func<double, double, double> func2 = (a, b) => { return a + b; };
  5. var res2 = func2(23.6, 566.6);
  6. Console.WriteLine(res2);
  7. Console.ReadKey();

C#语言入门详解-030泛型、partial类、枚举、结构 - 图6

C#语言入门详解-030泛型、partial类、枚举、结构 - 图7

Partial类

暂缺

枚举

  1. class Program {
  2. static void Main(string[] args)
  3. {
  4. Person person=new Person();
  5. person.Level = Level.Employee;
  6. Person boss=new Person();
  7. person.Name = "Tim";
  8. person.Skill = (Skill)7;
  9. boss.Level = Level.Boss;
  10. Console.WriteLine((int)Level.Employee);
  11. Console.WriteLine((person.Skill & Skill.Cook) ==Skill.Cook);//比特位用法,捕捉设置就是采用这种方法
  12. Console.ReadKey();
  13. }
  14. }
  15. enum Level
  16. {
  17. Employee=100,
  18. Manager=200,
  19. Boss=300,
  20. BigBoss=400,
  21. }
  22. enum Skill
  23. {
  24. Drvie=1,
  25. Cook=2,
  26. Program=4,
  27. Teach=8,
  28. }
  29. class Person
  30. {
  31. public int ID { get; set; }
  32. public string Name { get; set; }
  33. public Level Level { get; set; }
  34. public Skill Skill { get; set; }
  35. }

C#语言入门详解-030泛型、partial类、枚举、结构 - 图8

结构体类型

  1. using System;
  2. using System.Collections.Generic;
  3. namespace _030_泛型_partial_枚举_结构体 {
  4. class Program {
  5. static void Main(string[] args)
  6. {
  7. Student stu = new Student() { Id = 101, Name = "Tim" };
  8. object obj = stu;//装箱
  9. Student stu2 = (Student)obj;//拆箱
  10. stu2.Id = 102;
  11. stu2.Name = "Mike";
  12. Console.WriteLine($"#{stu.Id} Name:{stu.Name}");
  13. Console.WriteLine($"#{stu2.Id} Name:{stu2.Name}");
  14. stu2.Speak();
  15. Student stu3 = new Student(103, "Jack");
  16. stu3.Speak();
  17. Console.ReadKey();
  18. }
  19. }
  20. interface ISpeak {
  21. void Speak();
  22. }
  23. struct Student : ISpeak {
  24. public Student(int id, string name)
  25. {//不能有无参的构造函数,但可以有带参数的
  26. Id = id;
  27. Name = name;
  28. }
  29. public int Id { get; set; }
  30. public string Name { get; set; }
  31. public void Speak()//结构可以调用接口,但不能由其他类或结构体派生
  32. {
  33. Console.WriteLine($"Im #{this.Id} Name:{this.Name}!!!!");
  34. }
  35. }
  36. }

C#语言入门详解-030泛型、partial类、枚举、结构 - 图9

刘老师邮箱:timpthy.liu@outlook.com