泛型
Class ClassName<类型参数>
类型参数TParaName
泛型使用之前需要进行特化。
泛型类(泛型和面向对象的关系)
泛型特化之后是强类型的,能够看到Color的属性
using System;
namespace Generic
{
class Program
{
static void Main(string[] args)
{
Apple apple = new Apple() { Color = "Red" };
Book book = new Book() { Name = "WPF" };
Box<Apple> applebox = new Box<Apple>() { Cargo = apple };//泛型特化之后是强类型的,能够看到Color的属性
Console.WriteLine(applebox.Cargo.Color);
}
}
class Apple
{
public string Color { get; set; }
}
class Book
{
public string Name { get; set; }
}
class Box<TCargo>
{
public TCargo Cargo { get; set; }
}
}
泛型接口(泛型和面向对象,数据结构的关系)
两种方式:
- 在new实例的时候特化
- 在实现接口的时候特化
泛型接口集中名称空间:System.Collections.Generic
Generic Example1
using System;
namespace GenericInterface
{
class Program
{
static void Main(string[] args)
{
Student<int> student = new Student<int>();
student.ID = 101; //TID被特化成了int类型
student.Name = "Timonthy";
}
}
interface IUnique<TID> //确保唯一性,但是不知道唯一性的类型是什么
{
TID ID { get; set; }
}
class Student<TID> : IUnique<TID> //一个类实现泛型接口本身也需要是泛型类
{
public TID ID { get; set; } //实现泛型接口的属性
public string Name { get; set; }
}
}
Generic Example2
using System;
using System.Collections.Generic;
namespace GenericExample1
{
class Program
{
static void Main(string[] args)
{
IList<int> list = new List<int>();
for (int i = 0; i < 100; i++)
{
list.Add(i);
}
foreach (var item in list)
{
Console.WriteLine(item);
}
}
}
}
Generic Example3:带多个泛型参数的接口
using System;
using System.Collections.Generic;
namespace GenericExample1
{
class Program
{
static void Main(string[] args)
{
IDictionary<int, string> dict = new Dictionary<int, string>();//多态,特化后接口类型的变量引用一个特化后接口类型的实例
dict[1] = "Timonthy";
dict[2] = "Michael";
Console.WriteLine($"Student #1 is {dict[1]},Student #2 is {dict[2]}");
}
}
}
泛型方法(泛型和算法的关系)
方法重载Dug有风险,泛型登场
修饰符 T 方法名
声明泛型方法:
- 泛型方法有两个参数列表
- 封闭在圆括号内的方法参数列表
- 封闭在尖括号内的类型参数列表
- 要声明泛型方法,需要
- 在方法名称之后和方法参数列表之前放置类型参数列表;
- 在方法参数列表后放置可选的约束子句
```csharp
Example1:
static int[] Zip(int[] a,int[]b){}
staitc T[] Zip
(T[]a,T[]b){}
Example2:
public T PrintData(S s,T t) where S:Person{return s;}
<a name="ce36I"></a>
## 泛型委托
Action Func<br />delegate R Mydelegate<T,R>(T value);<br />无返回值:
```csharp
using System;
namespace GenericDelegate
{
class Program
{
static void Main(string[] args)
{
Action<string> action =Say;
action.Invoke("Timonthy");
Action<int> action1 = Mul;
action1.Invoke(1);
}
static void Say(string str)
{
Console.WriteLine($"Hello,{str}");
}
static void Mul(int x)
{
Console.WriteLine(x*100);
}
}
}
有返回值
using System;
namespace GenericDelegate
{
class Program
{
static void Main(string[] args)
{
Func<int, int, int> func = Add;
var result = func.Invoke(1, 2);
Console.WriteLine(result);
}
static int Add(int a,int b) {
return a + b;
}
static double Add(double a,double b) {
return a + b;
}
}
}
Lamda表达式
对于逻辑非常简单的方法不想去声明它,随时调用随时声明,匿名声明。
(a,b)带入到=>后面去实现 a,b的类型在Func
using System;
namespace Lamada {
class Program {
static void Main(string[] args) {
Func<int, int, int> func = (a, b) => { return a + b; };
var result = func.Invoke(1,2);
Console.WriteLine($"{result}");
}
}
}
partial类:分部类
partial类的好处
- 减少类的派生
- partial类与Entity Framework 实体框架
partial类与Windows Form,WPF,ASP.NET Core
实例
Example1:ADO .NET Entity Model
保证名称空间和类名相同
.NETFramework可以看见,.NETCore只支持第一代
刷新的话下面这个类会被重新生成,如果想在里面实现方法,则需要用分部类实现。namespace TestDataBase { class Program { static void Main(string[] args) { var dbContext = new BookstoreEntities(); var books = dbContext.Book; foreach (var item in books) { Console.WriteLine(item.Name); } } } }
分部类实现: ```csharp namespace TestDataBase { using System; using System.Collections.Generic;
public partial class Book {
public string Report() { return $"#{this.ID} Name:{this.Name} Price:{this.Price}"; }
} }
主程序:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestDataBase {
class Program {
static void Main(string[] args) {
var dbContext = new BookstoreEntities();
var books = dbContext.Book;
foreach (var item in books) {
Console.WriteLine(item.Report());
}
}
}
}
Example:partial类允许不同的部分使用不同的编程语言
Winform:partial类都是用C#写的
WPF:C# XAML
ASP .NET Core
枚举
- 人为限定取值范围的整数
- 整数值的对应
- 比特位式用法 读写文件等 ```csharp using System;
namespace Manager { class Program { static void Main(string[] args) { Person person = new Person(); person.Level = Level.Employee; person.Skill = Skill.Drive|Skill.Cook|Skill.Program|Skill.Teach; //或上,值为15 Console.WriteLine((person.Skill&Skill.Drive)>0);//与判断是否包含
Person boss = new Person();
boss.Level = Level.BigBoss;
Console.WriteLine((int)Level.Manager);
}
}
enum Level {
Employee=100,
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; }
}
}
结构体
- 值类型,可装/拆箱 Object o = stu (从栈中拷贝实例到堆中)
- 可实现接口,不能派生自类/结构体
- 不能有显示无参数构造器