在c#中,where关键字主要有两种用途
1、在泛型约束中限制类型参数
2、在linq查询中筛选数据
本文主要介绍where关键字在在泛型约束中的使用
泛型定义中的 where
子句指定对用作泛型类型、方法、委托或本地函数中类型参数的参数类型的约束。通过使用 where
关键字和泛型约束,可以创建更安全、更灵活的泛型类和方法的实现。
使用的对象
where可以对类、方法、委托和接口使用。
类的使用,例如c#中常用的list<t>
方法的使用,
public void myfunc<t>() where t:struct
委托的使用
public delegate void mydelete<t>() where t : class;
接口的使用
约束分类
约束 | 说明 |
where t : struct | t必须是值类型,如int,double等 |
where t : class | t必须是引用类型,如string,list<t>等 |
where t : new() | t必须有无参构造函数,即t参数可以通过使用new关键字创建实例。当与其他约束一起使用时,new() 约束必须最后指定。 |
where t : 基类 | t 必须继承自某个基类 |
where t : 接口 | t 必须实现某个接口 |
where t : u | t 必须派生自 u |
where t : struct
public class myclass<t> where t : struct { public t value { get; set; } }
例子:
// 只接受值类型的泛型方法 public class valuecalculator<t> where t : struct { public t add(t a, t b) { return (dynamic)a + (dynamic)b; // 简单示例,实际中需要更安全的实现 } } // 使用示例 class program { static void main() { var intcalc = new valuecalculator<int>(); console.writeline(intcalc.add(5, 3)); // 输出 8 // 下面这行会编译错误,因为 string 是引用类型 // var stringcalc = new valuecalculator<string>(); } }
where t : class
public class myclass<t> where t : class { public t value { get; set; } }
例子
public class repository<t> where t : class, new() { private list<t> items = new list<t>(); public t createitem() { var newitem = new t(); // 可以实例化,因为有 new() 约束 items.add(newitem); return newitem; } public void displaycount() { console.writeline($"items count: {items.count}"); } } // 使用示例 class program { class customer { public string name { get; set; } } static void main() { var repo = new repository<customer>(); var customer = repo.createitem(); customer.name = "john doe"; repo.displaycount(); // 输出 items count: 1 } }
where t : new()
public class factory<t> where t : new() { public t createinstance() { return new t(); } }
where t : 基类
public class animalshelter<t> where t : animal { public void shelter(t animal) { animal.feed(); } }
where t : 接口
public class sorter<t> where t : icomparable<t> { public void sort(t[] array) { array.sort(array); } }
例子
using system; // 定义一个接口 public interface idisplayable { void display(); } // 实现接口的类 public class product : idisplayable { public string name { get; set; } public void display() { console.writeline($"product: {name}"); } } // 使用 where 约束确保 t 实现 idisplayable public class displaymanager<t> where t : idisplayable { public void show(t item) { item.display(); // 安全调用,因为知道 t 有 display 方法 } } // 使用示例 class program { static void main() { var product = new product { name = "laptop" }; var manager = new displaymanager<product>(); manager.show(product); } }
where t : u
public class derivedcontainer<t, u> where t : u { // t 必须继承自 u 或实现 u(如果 u 是接口) }
例子
public class animal { public virtual void makesound() { console.writeline("some animal sound"); } } public class dog : animal { public override void makesound() { console.writeline("bark!"); } } // 约束 t 必须继承自 animal public class animalshelter<t> where t : animal { public void letanimalmakesound(t animal) { animal.makesound(); // 可以调用 animal 的方法 } } // 使用示例 class program { static void main() { var shelter = new animalshelter<dog>(); shelter.letanimalmakesound(new dog()); // 输出 "bark!" } }
多重约束
可以为类型参数指定多个约束
需要注意的是:当与其他约束一起使用时,new() 约束必须最后指定。
public class myclass<t> where t : class, idisposable, new() { }
多个类型参数的约束
对于多个类型参数,每个都可以有自己的约束:
public class myclass<tkey, tvalue> where tkey : icomparable<tkey> where tvalue : class,new() { }
约束的优点
1、增强类型安全 - 编译器可以在编译时捕获类型不匹配的错误。
2、减少运行时转换 - 避免不必要的类型检查和转换。
3、启用更多操作 - 知道类型参数具有某些特性(如特定方法或构造函数)后,可以在泛型代码中 使用这些特性。
4、不会对性能产生可测量的影响。
总结:合理使用泛型约束可以显著提高代码质量和安全性,但应该避免过度使用导致不必要的复杂性。在大多数情况下,优点远大于缺点,特别是在开发库代码或框架时。
到此这篇关于c# where 泛型约束的实现的文章就介绍到这了,更多相关c# where 泛型约束内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论