博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
泛型约束
阅读量:4635 次
发布时间:2019-06-09

本文共 4080 字,大约阅读时间需要 13 分钟。

using System;using System.Collections.Generic;using System.IO;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;//泛型命名空间: System.Collections.Generic//如: List
、IComparable
//参考:https://msdn.microsoft.com/zh-cn/library/sx2bwtw7.aspx//http://biancheng.dnbcw.info/net/358503.htmlnamespace WpfApplication1{ //这个泛型类只接受值类型的泛型参数 public class MyClass2 { } ///
/// MainWindow.xaml 的交互逻辑 /// public partial class MainWindow : Window { #region T:结构 ///
/// 调用: /// float _float = StructA
(1); //结果:2.0,约束为返回float类型 ///
///
约束T
///
int ///
返回T
private T StructA
(int arg) where T : struct { double db = 1.0 + arg; return (T)Convert.ChangeType(db, typeof(T)); } ///
/// 调用: /// int _int = StructB
(1); //结果:2,约束为返回int类型 ///
///
约束T
///
任意类型 ///
返回T
private T StructB
(T arg) where T : struct { double db = 1.0 + (double)Convert.ChangeType(arg, typeof(double)); return (T)Convert.ChangeType(db, typeof(T)); } ///
/// 调用: /// float? y = 1; /// float _float = StructC(y); //结果:2.0,约束为返回float类型 /// ///
约束T
///
任意可空类型 ///
返回T
private T StructC
(T? arg) where T : struct { double db = 1.0 + (double)Convert.ChangeType(arg, typeof(double)); return (T)Convert.ChangeType(db, typeof(T)); } #endregion private T Foo
(T arg) where T : class { double db = 1.0 + (double)Convert.ChangeType(arg, typeof(double)); return (T)Convert.ChangeType(db, typeof(T)); } public MainWindow() { InitializeComponent(); /*泛型模板实例化*/ //T:class 实现
<类别>
gci = new 实现
<类别>
(); gci.Insert(new 类别() { _int = 1, _float = 2, _double = 3.0 }); //T:new() InstanceA
testa = new InstanceA
("hello"); string str = testa.obj; //调用委托 InstanceA
.StackDelegate ds = DoWork; InstanceB
<类别>
testb = new InstanceB
<类别>
(); //委托 //调用方式一 DelegateT
dt = Method; string dts = dt("hello"); //结果:hello //调用方式二 DelegateT
d = new DelegateT
(Method); dts = dt("hello"); } static string Method(string s) { return s; } delegate string DelegateT
(T value); delegate T MyDelegate
() where T : new(); private static void DoWork(float[] items) { } //自定义泛型约束 //public bool MyMethod
(T t) where T : IMyInterface { } } #region T:new() //
为任意类型 public class InstanceA
{ public T obj{set;get;} public InstanceA(T obj) { this.obj = obj; } //创建泛型实例对像 public static T Instance() { T t = System.Activator.CreateInstance
(); return t; } public delegate void StackDelegate(T[] items); } //where T:new()指明了创建T的实例时应该具有构造函数。 //new()约束,要求类型参数必须提供一个无参数的构造函数。 public class InstanceB
where T : new() { public static T Instance() { T t = new T(); return t; } } //where T: 类别,T表示类型变量是继承于“类别” public class InstanceC
where T : 类别 { } #endregion #region T:class interface 接口
where T : class { void Insert(T entity); void Update(T entity); } public class 实现
: 接口
where T : class { public void Insert(T entity) { } public void Update(T entity) { } } public class 类别 { public 类别() { } public int _int { set; get; } public float _float { set; get; } public double _double { set; get; } } #endregion // 传人T类型,但T类型必须是IComparable类型 public class GenericClass
where T : IComparable { } //传人T和U类型,但T类型必须是class类型,U类型必须是struct类型 public class MyClass
where T : class where U : struct{ } }

 

转载于:https://www.cnblogs.com/sntetwt/p/5353362.html

你可能感兴趣的文章
chrome用type=file获取图片路径并转base64字符串
查看>>
Web项目替换jar包中的文件的方法
查看>>
【模式识别与机器学习】——3.9势函数法:一种确定性的非线性分类方法
查看>>
Alpha 冲刺五
查看>>
Python Matplotlib.plot Update image Questions
查看>>
杂记-字符串的字节长度
查看>>
Java面试——线程池
查看>>
h5页面点击事件ios没反应 移动端兼容性问题
查看>>
INSERT ... ON DUPLICATE KEY UPDATE Syntax
查看>>
加载静态文件,父模板的继承和扩展
查看>>
结对项目——Subway
查看>>
Angular Redux
查看>>
eventBus 与fragment
查看>>
net&nbsp;与&nbsp;javascript脚本的几种交互方法
查看>>
crontab 提示 command not found 解决方案
查看>>
洛谷P4114 Qtree1(树链剖分+线段树)
查看>>
P4718 【模板】Pollard-Rho算法
查看>>
点击表格弹窗获取另外一套数据之后,原表格相关数据的调用
查看>>
学校选址_洛谷U3451_带权中位数
查看>>
关于C/C++中的“auto”关键字
查看>>