泛型
- 为什么需要:避免成员膨胀或者类型膨胀
- 正交性:泛型类型(类/接口/委托/…)、泛型成员(属性/方法/字段/…)
- 类型方法的推断
- 泛型与委托、lambda表达式
partial类
- 减少类的派生
- partial类与Entity Framework
- partial类与windows forms,WPF。ASP.Net core
类型膨胀
如果有1k种产品,那么就有1k种盒子,显然是有问题的
public class Program
{
static void Main(string[] args)
{
Apple apple = new Apple() { Color = "Red" };
AppleBox box = new AppleBox();
box.Cargo = apple;
Console.WriteLine(box.Cargo.Color);
Book book = new Book() { Name = "New Book" };
BookBox bookBox = new BookBox() { Cargo = book };
Console.WriteLine(bookBox.Cargo.Name);
}
}
class Apple
{
public string Color { get; set; }
}
class Book
{
public string Name { get; set; }
}
class AppleBox
{
public Apple Cargo { get; set; }
}
class BookBox
{
public Book Cargo { get; set; }
}
成员膨胀
如果将来有1k种产品,那么这个类里面就有1k个成员
class Box
{
public Apple Apple { get; set; }
public Book Book { get; set; }
}
泛型
public class Program
{
static void Main(string[] args)
{
Apple apple = new Apple() { Color = "Red" };
Book book = new Book() { Name = "New Book" };
Box<Apple> box1 = new Box<Apple>() { Cargo = apple };
Box<Book> box2 = new Box<Book>() { Cargo = book };
}
}
class Apple
{
public string Color { get; set; }
}
class Book
{
public string Name { get; set; }
}
//解决了类型膨胀和成员膨胀,在main里出现的TCargo的为止都会被换
class Box<TCargo>
{
public TCargo Cargo { get; set; }
}
泛型接口
public class Program
{
static void Main(string[] args)
{
Student<int> stu = new Student<int>();
stu.ID = 101;
stu.Name = "Timothy";
}
}
interface IUnique<TId>
{
TId ID { get; set; }
}
class Student<TId> : IUnique<TId>
{
public TId ID { get; set; }
public string Name { get; set; }
}
泛型接口(2个参数例子)
static void Main(string[] args)
{
IDictionary<int, string> dict = new Dictionary<int, string>();
dict[1] = "Timothy";
dict[2] = "Michael";
Console.WriteLine($"Student #1 is {dict[1]}");
Console.WriteLine($"Student #2 is {dict[2]}");
}
泛型方法
static void Main(string[] args)
{
}
//将<>放在方法名的后面
static T[] Zip<T>(T[] a, T[] b)
{
T[] zipped = new T[a.Length + b.Length];
int ai = 0, bi = 0, zi = 0;
do
{
if (ai < a.Length) zipped[zi++] = a[ai++];
if (bi < b.Length) zipped[zi++] = b[bi++];
} while (ai < a.Length || bi < b.Length);
return zipped;
}
泛型委托
static void Main(string[] args)
{
Func<int, int, int> func1 = Add;
var result = func1(100, 200);
Console.WriteLine(result);
}
static int Add(int a,int b)
{
return a + b;
}
static double Add(double a,double b)
{
return a + b;
}
泛型委托+lamdba
static void Main(string[] args)
{
//double 可以删了
Func<double, double, double> func1 = (double a, double b) => { return a + b; };
var result = func1(100.1, 200.2);
Console.WriteLine(result);
}
- partial的理解:在一个已经固定的基类里还需要再添加一些新的内容
枚举
- 人为限制取值范围的整数
- 整数值的对应
- 比特位式用法
public class Program
{
static void Main(string[] args)
{
Person person = new Person();
person.Level = Level.Employee;
Person boss = new Person();
boss.Level = Level.Boss;
Console.WriteLine(boss.Level > person.Level);
Console.WriteLine((int)Level.Employee);
Console.WriteLine(Level.Manager);
person.Name = "Timothy";
person.Skill = Skill.Drive | Skill.Cook | Skill.Program | Skill.Teach;
Console.WriteLine((person.Skill & Skill.Cook) == Skill.Cook);
}
}
enum Level
{
Employee,
Manager,
Boss,
BigBoss,
}
//比特位式用法
enum Skill
{
Drive=1,
Cook=2,
Program=4,
Teach=8,
}
class Person
{
public int ID { get; set; }
public string Name { get; set; }
public Level Level { get; set; }
public Skill Skill { get; set; }
}
结构体
public class Program
{
static void Main(string[] args)
{
Student student = new Student() { ID = 101, Name = "Timtohy" };
//拆箱、装箱
object obj = student;
Student student2 = (Student)obj;
Console.WriteLine($"{student2.ID} Name:{student2.Name}");
student.Speak();
}
}
interface ISpeak
{
void Speak();
}
//结构体
struct Student:ISpeak
{
public int ID { get; set; }
public string Name { get; set; }
public void Speak()
{
Console.WriteLine($"I'm #{this.ID} student {this.Name}");
}
}