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插件的时候,发现老外实现的一个泛型对象池,觉得设计的小巧实用,不敢私藏,特此共享出来. 以前也看过很多博友关于对象池的总结分享,但是世界这么大,这么复杂到底什么样 ...
随机推荐
- 【漏洞分析】KaoyaSwap 安全事件分析
相关信息 KaoyaSwap 是 BSC 链上的一个自动做市商 AMM.然后,现在他们的官网 https://www.kaoyaswap.com/ 已经打不开了(如果我打开方式没错的话).所以就直接进 ...
- JDBC连接池&JDBCTemplate
今日内容 1. 数据库连接池 2. Spring JDBC : JDBC Template 数据库连接池 1. 概念:其实就是一个容器(集合),存放数据库连接的容器. 当系统初始化好后,容器被创建,容 ...
- 没有二十年功力,写不出Thread.sleep(0)这一行“看似无用”的代码!
你好呀,我是喜提七天居家隔离的歪歪. 这篇文章要从一个奇怪的注释说起,就是下面这张图: 我们可以不用管具体的代码逻辑,只是单单看这个 for 循环. 在循环里面,专门有个变量 j,来记录当前循环次数. ...
- KingbaseES V8R6C5B041 sys_backup.sh单实例备份案例
数据库版本: test=# select version(); version ---------------------------------------------------------- ...
- Springboot_maven多环境配置
开发过程中总是需要多环境配置,而Spring自带的方式不是那么优秀,可以利用maven来帮助做到 可以再pom.xml中配置profiles来做到 打包命令: mvn clean package -P ...
- 基于 PyTorch 和神经网络给 GirlFriend 制作漫画风头像
摘要:本文中我们介绍的 AnimeGAN 就是 GitHub 上一款爆火的二次元漫画风格迁移工具,可以实现快速的动画风格迁移. 本文分享自华为云社区<AnimeGANv2 照片动漫化:如何基于 ...
- java~springboot(2022之后)~目录索引
回到占占推荐博客索引 最近写了不过关于java,spring,微服务的相关文章,今天把它整理一下,方便大家学习与参考. java~springboot(2022之前)~目录索引 java~spring ...
- Kubernetes 监控--PromQL
Prometheus 通过指标名称(metrics name)以及对应的一组标签(label)唯一定义一条时间序列.指标名称反映了监控样本的基本标识,而 label 则在这个基本特征上为采集到的数据提 ...
- Ingress资源规范
k8s v1.19版本中Ingress资源规范从v1beta1版本升级至稳定的v1版本 v1beta1版本 v1beta1版本的Ingress资源位于API群组的extensions之中,该版本的资源 ...
- img和div之间有间隙的原因及解决方法
div 中 存在 img标签,由于img标签的 display:inline-block 属性. #####display:inline-block布局的元素在chrome下会出现几像素的间隙,原因是 ...