where关键字的用法

where关键词一个最重要的用法就是在泛型的声明、定义中做出约束。
约束又分为接口约束、基类约束、构造函数约束、函数方法的约束。

1.接口约束,泛型参数必须实现相应的接口才可以

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace where约束程序
{
public class Student : IStudent
{
private string name;
private int id;
//实现IStudnet接口
public string GetName
{
get
{
return name;
}
} public int GetID
{
get
{
return id;
}
}
} //接口约束,T必须是实现了IStudent接口的类
public class MyClass1<T> where T: IStudent
{
public MyClass1()
{
Console.WriteLine("接口约束构造成功!");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace where约束程序
{
public interface IStudent
{
string GetName{ get; }
int GetID { get; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace where约束程序
{
class Program
{
static void Main(string[] args)
{
MyClass1<Student> myclass1 = new MyClass1<Student>(); Console.ReadKey();
}
}
}

输出结果

接口约束构造成功!

2.基类约束,类型参数必须是指定的基类或派生自指定的基类,多用于继承体系之下

3.构造函数约束,对构造函数有一定的约束

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace where约束程序
{
public class Teacher:People
{
public Teacher() { }
public Teacher(string name,int age,string sex):base(name,age,sex)
{ }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace where约束程序
{
public class People
{
public int Age { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public People() { }
public People(string name,int age,string sex)
{
Name = name;
Age = age;
Sex = sex;
}
} //基类约束,T必须是People或者People的子类
public class MyClass2<T> where T:People
{
public MyClass2()
{
Console.WriteLine("基类约束构造成功");
}
} //构造函数约束,T必须是引用类型,且必须具有无参构造函数
public class MyClass3<T> where T : class,new()
{
public MyClass3()
{
Console.WriteLine("构造函数约束构造成功");
}
} }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace where约束程序
{
class Program
{
static void Main(string[] args)
{
MyClass2<Teacher> myclass2 = new MyClass2<Teacher>();
MyClass3<Teacher> myclass3 = new MyClass3<Teacher>();
//MyClass3<Student> myclass4 = new MyClass3<Student>();//由于Studnet中没有显示的声明构造函数,所以会自动生成一个默认的无参构造函数
Console.ReadKey();
}
}
}

总结:

  • where T : struct 这表明T必须是一个值类型,像是int,decimal这样的
  • where T : class 这表明T必须是一个引用类型,像是自定义的类、接口、委托等
  • where T : new() 这表明T必须有无参构造函数,且如果有多个where约束,new()放在最后面
  • where T : [base class name] 这表明T必须是base class类或者其派生类
  • where T : [interface name] 这表明T必须实现了相应的接口

C#where关键字约束的更多相关文章

  1. 详解如何定义SQL Server外关键字约束

    SQL Server外关键字约束定义了表之间的关系.当一个表中的一个列或多个列的组合和其它表中的主关键字定义相同时,就可以将这些列或列的组合定义为外关键字,并设定它适合哪个表中哪些列相关联.这样,当在 ...

  2. Oracle--数据库中的五种约束

    数据库中的五种约束 数据库中的五种约束及其添加方法 五大约束 1.--主键约束(Primay Key Coustraint) 唯一性,非空性  2.--唯一约束 (Unique Counstraint ...

  3. [SQL Server系] -- 约束

    什么是约束? 约束(Constraint)是SQL Server中提供的 自动保存数据库完整性 的一种方法,定义了可输入表或表的列中的数据限制条件. SQL Server中共有5中约束 PRIMARY ...

  4. 经典SQL语句大全_主外键_约束

    一.基础(建表.建约束.关系) 约束(Constraint)是Microsoft SQL Server 提供的自动保持数据库完整性的一种方法,定义了可输入表或表的单个列中的数据的限制条件(有关数据完整 ...

  5. 【SQL】数据库中的五种约束

    #五大约束 1.主键约束(Primay Key Coustraint) 唯一性,非空性 2.唯一约束 (Unique Counstraint)唯一性,可以空,但只能有一个 3.检查约束 (Check ...

  6. 约束Constraints--主键约束、外键约束、唯一约束、检查约束、默认约束、NOT NULL约束、列约束与表约束、创建约束、删除约束

    约束   Including Constraints 以下内容转自:https://www.cnblogs.com/wcl2017/p/7043939.html和http://blog.csdn.ne ...

  7. 让我们用心感受泛型接口的协变和抗变out和in

    关键字out和in相信大家都不陌生,系统定义的很多泛型类型大家F12都或多或少看见了.但是实际中又很少会用到,以前在红皮书里看到,两三页就介绍完了.有的概念感觉直接搬出来的,只是说这样写会怎样,并没有 ...

  8. 泛型类型的协变(covariant)和逆变

    官网:http://msdn.microsoft.com/zh-cn/library/dd799517.aspx 原文链接:http://book.51cto.com/art/201112/30857 ...

  9. Unity3D 基于预设(Prefab)的泛型对象池实现

    背景 在研究Inventory Pro插件的时候,发现老外实现的一个泛型对象池,觉得设计的小巧实用,不敢私藏,特此共享出来. 以前也看过很多博友关于对象池的总结分享,但是世界这么大,这么复杂到底什么样 ...

随机推荐

  1. Android Module配置C++支持

    AndroidStudio提供了创建项目时选择C++支持的模板,但是新建Module的时候并没有C++模板, 要如何配置Module的C++支持呢? 虽然Module没有提供C++模板,但是我们可以手 ...

  2. 【Traefik二次开发】服务 Service 开发

    Service 定义 https://doc.traefik.io/traefik/routing/services/ The Services are responsible for configu ...

  3. Helm安装ingress-nginx-4.0.19

    Application version 1.1.3 Chart version 4.0.19 获取chart包 helm fetch ingress-nginx/ingress-nginx --ver ...

  4. [SDR] GNU Radio 系列教程(二) —— 绘制第一个信号分析流程图

    目录 1.前言 2.启动 GNU Radio 3.新增块 4.运行 本文视频 参考链接 1.前言 本文将介绍如何在 GNU Radio 中创建和运行第一个流程图. 2.启动 GNU Radio GNU ...

  5. KingbaseES V8R6 锁等待检测

    背景 对于多数数据库,dba技能之一就是查找锁.锁的存在有效合理的在多并发场景下保证业务有序进行.下面我们看一下KingbaseESV8R6中查找阻塞的方法. 1.找到"被阻塞者" ...

  6. 【读书笔记】C#高级编程 第二十二章 安全性

    (一)身份验证和授权 安全性的两个基本支柱是身份验证和授权.身份验证是标识用户的过程,授权在验证了所标识用户是否可以访问特性资源之后进行的. 1.标识和Principal 使用标识可以验证运行应用程序 ...

  7. Python 第三次实验

    一如既往地简单,不到半个小时即可完成 [1] (程序设计)输入一个正整数,输出它的因子分解式.如输入132,则输出132=122311 n=int(input()) print(1,end='') f ...

  8. Stream流式计算

    Stream流式计算 集合/数据库用来进行数据的存储 而计算则交给流 /** * 现有5个用户,用一行代码 ,一分钟按以下条件筛选出指定用户 *1.ID必须是偶数 *2.年龄必须大于22 *3.用户名 ...

  9. 分布式MinIO快速入门

    官方文档地址:http://docs.minio.org.cn/docs/master/distributed-minio-quickstart-guide Minio服务基于命令行传入的参数自动切换 ...

  10. vscode展示子文件夹

    取消勾选设置-功能-compact Folders