C#语言入门详解-030泛型、partial类、枚举、结构
- 泛型(generic)无处不在
为什么需要泛型:避免成员膨胀或者类型膨胀
正交性:泛型类型(类/接口/委托/……)、泛型成员(属性/方法/字段/……)
类型方法的参数推断
泛型与委托、lambda表达式
- partial类
减少派生的类
partial类与Entity Framework
partial类与Windows Forms、WPF、ASP.NET Core
- 枚举类型
人为限定取值范围的整数
整数值的对应
比特位式用法
- 结构体(struct)
值类型,可装/可拆
可实现接口,不能派生自类/结构体
不能有显示无参构造器
泛型类
using System;
namespace _030_泛型_partial类_枚举_结构体 {
class Program {
static void Main(string[] args)
{
var apple = new Apple() { Color = "Red" };
var book = new Book() { Name = "New Book" };
Box<Apple> box = new Box<Apple>() { Cargo = apple };
Box<Book> bookBox = new Box<Book>() { Cargo = book };
Console.WriteLine(box.Cargo.Color);
Console.WriteLine(bookBox.Cargo.Name);
Console.ReadKey();
}
}
class Apple {
public string Color { get; set; }
}
class Book {
public string Name { get; set; }
}
class Box<TCargo> {
public TCargo Cargo { get; set; }
}
}
泛型接口
using System;
namespace _030_泛型_partial类_枚举_结构体 {
class Program {
static void Main(string[] args)
{
Student<int> stu=new Student<int>();
stu.Id = 101;
stu.Name = "Tim";
Student<ulong> stu1=new Student<ulong>();
stu1.Id = 1000000000001;
stu1.Name = "Jack";
Stu2 stu2=new Stu2();
stu2.Id = 100000000000000;
stu2.Name = "Maomao";
}
}
interface IUnique<TId>
{
TId Id { get; set; }
}
class Student<TId> : IUnique<TId> {
public TId Id { get; set ; }
public string Name { get; set; }
}
class Stu2: IUnique<ulong> {
public ulong Id { get ; set; }
public string Name { get; set; }
}
}
泛型数组
static void Main(string[] args)
{
IList<int> list = new List<int>();
for (int i = 0; i < 100; i++)
{
list.Add(i);
}
foreach (int i in list)
{
Console.WriteLine(i);
}
Console.ReadKey();
}
static void Main(string[] args)
{
IDictionary<int,string> dict=new Dictionary<int, string>();
dict[1] = "Tim";
dict[2] = "Mack";
Console.WriteLine($"student #1 is {dict[1]}");
Console.WriteLine($"student #2 is {dict[2]}");
Console.ReadKey();
}
泛型方法
static void Main(string[] args)
{
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(a1, a2);
Console.WriteLine(string.Join(",", result));
var result2 = Zip(a3, a4);
Console.WriteLine(string.Join(",", result2));
Console.ReadKey();
}
private static T[] Zip<T>(T[] a1, T[] a2)
{
T[] zipped = new T[a1.Length + a2.Length];
int ai = 0, bi = 0, zi = 0;
do
{
if (ai < a1.Length) zipped[zi++] = a1[ai++];
if (bi < a2.Length) zipped[zi++] = a2[bi++];
} while (ai < a1.Length | bi < a2.Length);
return zipped;
}
泛型委托
static void Main(string[] args)
{
Action<string> a1 = Say;
a1("Tom");
Action<int> a2 = Mul;
a2(3);
Console.ReadKey();
}
static void Say(string str)
{
Console.WriteLine($"Hello {str}!");
}
static void Mul(int x)
{
Console.WriteLine(x * 100);
}
static void Main(string[] args)
{
Func<int, int, int> func = Add;
var res = func(100, 200);
Console.WriteLine(res);
Func<double, double, double> func2 = Add;
var res2 = func2(23.6, 566.6);
Console.WriteLine(res2);
Console.ReadKey();
}
static int Add(int a, int b)
{
return a + b;
}
static double Add(double a, double b)
{
return a + b;
}
Func<int, int, int> func = (int a, int b) => { return a + b; };//泛型与lamuda表达式配合使用
var res = func(100, 200);
Console.WriteLine(res);
Func<double, double, double> func2 = (a, b) => { return a + b; };
var res2 = func2(23.6, 566.6);
Console.WriteLine(res2);
Console.ReadKey();
Partial类
暂缺
枚举
class Program {
static void Main(string[] args)
{
Person person=new Person();
person.Level = Level.Employee;
Person boss=new Person();
person.Name = "Tim";
person.Skill = (Skill)7;
boss.Level = Level.Boss;
Console.WriteLine((int)Level.Employee);
Console.WriteLine((person.Skill & Skill.Cook) ==Skill.Cook);//比特位用法,捕捉设置就是采用这种方法
Console.ReadKey();
}
}
enum Level
{
Employee=100,
Manager=200,
Boss=300,
BigBoss=400,
}
enum Skill
{
Drvie=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; }
}
结构体类型
using System;
using System.Collections.Generic;
namespace _030_泛型_partial类_枚举_结构体 {
class Program {
static void Main(string[] args)
{
Student stu = new Student() { Id = 101, Name = "Tim" };
object obj = stu;//装箱
Student stu2 = (Student)obj;//拆箱
stu2.Id = 102;
stu2.Name = "Mike";
Console.WriteLine($"#{stu.Id} Name:{stu.Name}");
Console.WriteLine($"#{stu2.Id} Name:{stu2.Name}");
stu2.Speak();
Student stu3 = new Student(103, "Jack");
stu3.Speak();
Console.ReadKey();
}
}
interface ISpeak {
void Speak();
}
struct Student : ISpeak {
public Student(int id, string name)
{//不能有无参的构造函数,但可以有带参数的
Id = id;
Name = name;
}
public int Id { get; set; }
public string Name { get; set; }
public void Speak()//结构可以调用接口,但不能由其他类或结构体派生
{
Console.WriteLine($"Im #{this.Id} Name:{this.Name}!!!!");
}
}
}
刘老师邮箱:timpthy.liu@outlook.com