c# 泛型
关于c# 泛型关于的基本概念和应用以及有点网上太多不想多说,
今天先从泛型的约束讲起:
引用别人的文章介绍一下:
泛型约束简介
C#泛型要求对"所有泛型类型或泛型方法的类型参数"的任何假定,都要基于"显式的约束",以维护C#所要求的类型安全.
"显式约束"有where字句表达,可以指定"基类约束","接口约束","构造器约束","值类型/引用类型约束"共四中约束.
"显示约束"并非必须,如果没有指定"显式约束",泛型类型参数将只能访问System.Object类型中的公有方法.
基类约束
class A
{
public void F1(){}
}
class B
{
public void F2(){}
}
class C(S,T)
where S:A // S继承自A
where T:B // T继承自B
{
// 可以在类型为S的变量上调用F1
// 可以在类型为T的变量上调用F2
}
接口约束
interface IPrintable{void Print();}
interface IComparable<T>{int CompareTo(T v);}
interface IKeyProvider<T>{T HetKey();}
class Dictionary<K,V>
where K:IComparable<K>
where V:IPrintable,IKeyProvider<K>
{
// 可以在类型为K的变量上调用CompareTo
// 可以在类型为V的变量上调用Print和HetKey
}
构造器约束
class A
{
public A(){}
}
class B
{
public B(int i)()
}
class C<T>
where T:new()
{
// 可以在其中使用T t = new T();
}
C<A> c = new C<A>(); // 可以,A有无参数构造器
C<B> c = new C<B>(); // 错误,B没有无参数构造器
值类型/引用类型约束
public struct A{...}
public class B{...}
class C<T>
where T : struct
{
// T在这里面是一个值类型
}
C<A> c = new C<A>(); // 可以,A是一个值类型
C<B> c = new C<B>(); // 错误,B是一个引用类型
再看完以上介绍后:
class MyClass <T> where T : BaseClass, ISomeInterface
通过这个例子我们得知可以利用多个约束,但是需要注意的是泛型只允许定义一种类型约束,但可以定义多个接口约束,
并且该泛型 <T>的类型,必须同时满足两个条件:一、是BaseClass的派生类,二、实现ISomeInterface接口
而BaseClass与ISomeInterface没有必然的联系。
using System;
using System.Collections.Generic;
using System.Text;
namespace fanxing
...{
interface interface1
...{
int add(int a, int b);
}
class class1
...{
public int minus(int a, int b)
...{
return a - b;
}
}
class class2:class1,interface1
...{
public int add(int a, int b)
...{
return a + b;
}
}
class stack1<T> where T : class1, interface1
...{
public string plus(T t)
...{
return t.add(8,4).ToString()+"---"+t.minus(8,4).ToString();
}
}
class Program
...{
static void Main(string[] args)
...{
class2 intef1 = new class2();
stack1<class2> st = new stack1<class2>();
Console.Write(st.plus(intef1));
Console.Read();
}
}
}
得到结果是12--4,
这里为什么会需要多个约束呢?它的作用什么呢?
首先我们可以看
class stack1<T> where T : class1, interface1
...{
public string plus(T t)
...{
return t.add(8,4).ToString()+"---"+t.minus(8,4).ToString();
}
}这个时候]你可以利用t来引出add和minus两种方法在泛型类内部使用,如果没有这样操作的话,
泛型类T它是不会知道引出哪种方法的,大家可以单独测试如果单独继承约束接口就只能引出add方法,
单独约束类只能引出minus方法,为了在泛型类内部能够使用到T完整的所有方法,所以我们想到了
多个约束。还可以与下面作测试大家看:
还有一种约束是构造器约束,
构造器约束就是new(),可再泛型类内部通过实例化new T()从而得到对象,大家可以看我定义的两种
泛型类,第一种是不能引出相应的方法的,因为它不知道这个T是谁,第二个就可以得到相应的class1中的
方法。
using System;
using System.Collections.Generic;
using System.Text;
namespace fanxing
...{
interface interface1
...{
int add(int a, int b);
}
class class1
...{
public int minus(int a, int b)
...{
return a - b;
}
}
class class2:class1,interface1
...{
public int add(int a, int b)
...{
return a + b;
}
}
/**//*class stack1<T> where T :new()
{
public T mode()
{
T t = new T();
return t;
}
}*/
class stack1<T> where T : class1,new()
...{
public int mode()
...{
T t = new T();
return t.minus(8, 4);
}
}
class Program
...{
static void Main(string[] args)
...{
class2 intef1 = new class2();
stack1<class2> st = new stack1<class2>();
Console.Write(st.mode());
Console.Read();
}
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xiaolei1982/archive/2008/04/03/2247460.aspx
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述