当前位置:网站首页>C generic constraint
C generic constraint
2022-06-09 06:01:00 【Go_ Accepted】
1、 Generic constraint
Let the types of generics have certain restrictions
keyword :where
Generic constraints have a total of 6 Kind of :
Value type where Generic letters :struct Reference type where Generic letters :class There is a parameterless public constructor where Generic letters :new() A class itself or a derived class where Generic letters : Class name Derived type of an interface ( Derived types can be classes or interfaces ) where Generic letters : The interface name Another generic type itself or a derived type where Generic letters : Another generic letter
2、 Explanation of generic constraints
List part , Unlisted similar
(1) Value type constraint
class Test<T> where T:struct{ public T value; public void TestFun<K>(K v) where K:struct{ } }(2) Reference type constraints
class Test<T> where T:class{ public T value; public void TestFun<K>(K v) where K:class{ } }(3) No public participation Construction constraints
class Test<T> where T:new(){ public T value; public void TestFun<K>(K v) where K:new(){ } } public Test2{ // If the public nonparametric construct of this class is overridden by an overload , Or the parameterless construct is declared as private, Will report a mistake // This class cannot be an abstract class }Test<Test2> t = new Test<Test2>(); Test<int> t = new Test<int>(); // The nonparametric construction of a structure is not overridden , All structs have parameterless constructors(4) Another generic constraint
class Test<T,U> where T:U{ public T value; public void TestFun<K,V>(K k) where K :V{ } }among T And U The same or T yes U Derived types of ,K similar
3、 Combination of constraints
class Test<T> where T:class, new(){ }But be careful
class Test<T> where T : new(),class{ }Will report a mistake , therefore new() Usually written at the end
4、 Multiple generics have constraints
Followed by space where, The same format
class Test<T,K> where T:class, new() where K:struct{ }
5、 practice
Implement a singleton pattern base class with generics
// where T : new() Because instance = new T() Make sure T Type must have a parameterless constructor , But this will cause the constructor of the class to be public Of class SingleBase<T> where T : new(){ private static T instance = new T(); public static T Instance { get { return instance; } } } class Test : SingleBase<Test> { public int value = 10; }
边栏推荐
- unity 定位服务GPS API
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) D. Jon and Orbs
- Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) K - Send the Fool Further! (medi
- New and old versions of Minio tool classes and deployment documents
- 复杂流程挖掘技术隐藏于平民化产品背后-专访容智信息创始人柴亚团
- 使用点云数据创建数字高程模型(DEM)
- Entity to map tool
- Once, the online environment redis response was slow, causing an avalanche
- Ffmpeg pulls webrtc streams, and the first all open source solution in the metartc industry is coming
- cbnet环境配置和运行中遇到的问题
猜你喜欢

Detailed understanding and learning of transactions in MySQL (transaction management, transaction isolation level, transaction propagation mechanism)

SSL证书包含了哪些信息?

How to solve the garbled code in the query statement when MySQL uses fuzzy query for Chinese

Data governance: how to improve enterprise data quality?

Complete jitsi meet guide for webrtc

synchronized 详细解析

JVM basic theory.

Go language tutorial 02 go common libraries + compile consult

传输介质双绞线和光纤及二进制

srs-nodejs
随机推荐
Educational Codeforces Round 20 D. Magazine Ad
MySQL tuning - overview of MySQL parameters and status
js 获取url参数并且进行中文解析
VK cup 2017 - round 1 C. bear and tree jumpsdp
MVCC多版本控制
C# 委托相关
Thread interrupted detailed parsing
“==“和 equals 方法究竟有什么区别?
Dynamic data source
Topic24——3. 无重复字符的最长子串
MySQL master-slave replication (Linux centos7)
C# List排序
unity 定位服务GPS API
JVM basic theory.
What information does the SSL certificate contain?
synchronized 详细解析
Creating a basic duilib window process
Tensorflow introductory tutorial 02 basic concepts of tensorflow (data flow graph, tensor, tensorboard)
Jdbc-dbutils
VK Cup 2017 - Round 3 B. Dynamic Problem Scoring