泛型:
正交性:泛型和其它的编程实体都有正交点,导致泛型对编程的影响广泛而深刻。
与类、接口、字段、属性的结合:
class Demo_Generic
{
public void test()
{
Demo31_Apple demo31_Apple = new Demo31_Apple() { Color = "Red" };
Demo31_Box<Demo31_Apple> demo31_Box = new Demo31_Box<Demo31_Apple>() { Cargo = demo31_Apple};
Console.WriteLine(demo31_Box.Cargo.Color);
Demo31_Book demo31_Book = new Demo31_Book() { Name = "abc" };
Demo31_Box<Demo31_Book> demo31_Box2 = new Demo31_Box<Demo31_Book>() { Cargo = demo31_Book };
Console.WriteLine(demo31_Box2.Cargo.Name);
Student<int> student = new Student<int>() { Name = "Li", ID = 12301201 };
Console.WriteLine($"{student.ID}{student.Name}");
Student2 student2 = new Student2() { Name = "Wang", ID = 12301201 };
Console.WriteLine($"{student2.ID}{student2.Name}");
}
}
class Demo31_Apple
{
public string Color { get; set; }
}
class Demo31_Box<T>
{
public T Cargo { get; set; }
}
class Demo31_Book
{
public string Name { get; set; }
}
interface IUnique<TId>
{
TId ID { get; set; }
}
class Student<TId> : IUnique<TId>
{
public TId ID { get; set; }
public string Name { get; set; }
}
class Student2 : IUnique<int>
{
public string Name { get; set; }
public int ID { get; set; }
}
与委托、方法的结合:
public void test()
{
IDictionary<int, string> dict = new Dictionary<int, string>();
dict[1] = "a";
dict[2] = "b";
Console.WriteLine(dict[1]);
Console.WriteLine(dict[2]);
int[] a1 = { 1, 2, 3, 4, 5 };
int[] a2 = { 1, 2, 3, 4, 5, 6 };
double[] a3 = { 1.1, 2.2, 3.3, 4.4, 5.5 };
double[] a4 = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 };
var result = Zip(a3, a4);
Console.WriteLine(string.Join(",", result));
Action<string> action = Say;
action.Invoke("123");
Action<int> action2 = Mul;
action2(20);
Func<double, double,double> action3 = (a,b) => { return a+b; };//lamda与泛型的结合使用
Console.WriteLine(action3(1,2));
}
public static T[] Zip<T>(T[] a, T[] b){
T[] zipped = new T[a.Length + b.Length];
int ai = 0, bi = 0, zi = 0;
while (ai < a.Length || bi < b.Length)
{
if (ai < a.Length) zipped[zi++] = a[ai++];
if (bi < b.Length) zipped[zi++] = b[bi++];
}
return zipped;
}
private static void Say(string content)
{
Console.WriteLine($"Hello!{content}.");
}
private static void Mul(int x)
{
Console.WriteLine($"{x*100}");
}
partial类:
可以分开定义同一个类
枚举、结构体:
枚举类示例:
class Demo_Enum
{
public void test()
{
Demo31_Person demo31_Person = new Demo31_Person();
Console.WriteLine(demo31_Person.Level);
demo31_Person.Level = Demo31_Level.Manager;
Console.WriteLine((int)demo31_Person.Level);//Manager是101
Console.WriteLine((int)Demo31_Level.Boss);//Boss是102
Console.WriteLine(Demo31_Level.BigBoss > Demo31_Level.Boss);
demo31_Person.Name = "Timothy";
demo31_Person.Skill = Demo31_Skill.Cook | Demo31_Skill.Drive;
if (((int)demo31_Person.Skill & (int)Demo31_Skill.Cook) == (int)Demo31_Skill.Cook)//可以不用强转为int
{
Console.WriteLine($"He can {Demo31_Skill.Cook}");
}
}
}
enum Demo31_Level{
Employee = 100,
Manager,
Boss,
BigBoss,
}
enum Demo31_Skill
{
Drive = 1,
Cook = 2,
Program = 4,
Teach = 8,
}
class Demo31_Person
{
public int ID { get; set; }
public string Name { get; set; }
public Demo31_Level Level { get; set; }
public Demo31_Skill Skill { get; set; }
}
结构体就没什么好展示的了
结构体类型只能由接口类型派生,结构体无法作为基类