C#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关键字约束的更多相关文章
- 详解如何定义SQL Server外关键字约束
SQL Server外关键字约束定义了表之间的关系.当一个表中的一个列或多个列的组合和其它表中的主关键字定义相同时,就可以将这些列或列的组合定义为外关键字,并设定它适合哪个表中哪些列相关联.这样,当在 ...
- Oracle--数据库中的五种约束
数据库中的五种约束 数据库中的五种约束及其添加方法 五大约束 1.--主键约束(Primay Key Coustraint) 唯一性,非空性 2.--唯一约束 (Unique Counstraint ...
- [SQL Server系] -- 约束
什么是约束? 约束(Constraint)是SQL Server中提供的 自动保存数据库完整性 的一种方法,定义了可输入表或表的列中的数据限制条件. SQL Server中共有5中约束 PRIMARY ...
- 经典SQL语句大全_主外键_约束
一.基础(建表.建约束.关系) 约束(Constraint)是Microsoft SQL Server 提供的自动保持数据库完整性的一种方法,定义了可输入表或表的单个列中的数据的限制条件(有关数据完整 ...
- 【SQL】数据库中的五种约束
#五大约束 1.主键约束(Primay Key Coustraint) 唯一性,非空性 2.唯一约束 (Unique Counstraint)唯一性,可以空,但只能有一个 3.检查约束 (Check ...
- 约束Constraints--主键约束、外键约束、唯一约束、检查约束、默认约束、NOT NULL约束、列约束与表约束、创建约束、删除约束
约束 Including Constraints 以下内容转自:https://www.cnblogs.com/wcl2017/p/7043939.html和http://blog.csdn.ne ...
- 让我们用心感受泛型接口的协变和抗变out和in
关键字out和in相信大家都不陌生,系统定义的很多泛型类型大家F12都或多或少看见了.但是实际中又很少会用到,以前在红皮书里看到,两三页就介绍完了.有的概念感觉直接搬出来的,只是说这样写会怎样,并没有 ...
- 泛型类型的协变(covariant)和逆变
官网:http://msdn.microsoft.com/zh-cn/library/dd799517.aspx 原文链接:http://book.51cto.com/art/201112/30857 ...
- Unity3D 基于预设(Prefab)的泛型对象池实现
背景 在研究Inventory Pro插件的时候,发现老外实现的一个泛型对象池,觉得设计的小巧实用,不敢私藏,特此共享出来. 以前也看过很多博友关于对象池的总结分享,但是世界这么大,这么复杂到底什么样 ...
随机推荐
- KingbaseES R6 集群物理copy方式手工添加新备库节点
案例说明: 对于主库数据量比较大的环境,在添加新节点是可以采用在线clone方式创建新的备库节点,也可以在离线的状态下,直接拷贝其中一个备库的所有集群相关目录来创建新的备库节点.本案例介绍了通过离线物 ...
- Gitea 1.17.2 | 带来视觉提升、完善资源校验、加强安全性等42项优化
Gitea 1.17.2 合并了 42 个 Pull Request,现已正式发布,我们建议所有用户升级到此版本.您可以到阅读原文了解更详细的介绍. 致谢:@zeripath 为 Gitea 贡献了诸 ...
- .NET 实现启动时重定向程序运行路径及 Windows 服务运行模式部署
日常工作中有时候会遇到需要将程序直接在服务器上运行,而不依赖于 IIS 托管的情况,直接运行有两种方式,一种是部署为 服务模式,另一种则是 直接启动 .NET 发布之后的 exe 文件以 控制台模式运 ...
- 【Java面试】面试遇到宽泛的问题,这么回答就稳了,谈谈你对Redis的理解
"谈谈你对Redis的理解"! 面试的时候遇到这类比较宽泛的问题,是不是很抓狂? 是不是不知道从何开始说起? 没关系,今天我用3分钟教你怎么回答. 大家好,我是Mic,一个工作了1 ...
- Traefik 2.0 实现自动化 HTTPS
文章转载自:https://mp.weixin.qq.com/s?__biz=MzU4MjQ0MTU4Ng==&mid=2247484457&idx=1&sn=35112e98 ...
- frps服务端与nginx可共用443端口
转载自: https://www.ioiox.com/archives/78.html frps服务器上的nginx frps.ini配置 由于nginx占用80/443端口,frps.ini中的 v ...
- firewalld防火墙基本使用
FirewallD 是 CentOS 7 服务器上默认可用的防火墙管理工具.基本上,它是 iptables 的封装,有图形配置工具 firewall-config 和命令行工具 firewall-cm ...
- PHP全栈开发(一):CentOS 7 配置LAMP
服务器CentOS7 IP地址:10.28.2.249 进行网络配置 可以使用ip address命令查看当前的网卡状态 两张网卡,一张lo网卡一张ens160网卡 Ens160这个网卡的配置文件为/ ...
- POJ3311 Hie with the Pie(状压DP,Tsp)
本题是经典的Tsp问题的变形,Tsp问题就是要求从起点出发经过每个节点一次再回到起点的距离最小值,本题的区别就是可以经过一个节点不止一次,那么先预处理出任意两点之间的最短距离就行了,因为再多走只会浪费 ...
- Springboot集成阿里云短信
目录 1 前言 2 准备工作 2.1 了解流程 2.2 配置信息 2.3 短信签名和模板 2.3.1 签名 2.3.2 模板 2.3.3 存入数据库 3 SDK 4 集成Springboot 4.1 ...