关于C#泛型最重要的事情...教训 [英] Most important things about C# generics... lesson learned

查看:152
本文介绍了关于C#泛型最重要的事情...教训的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么都是你们知道关于泛型最重要的事情:隐藏的功能,常见的错误,最好,最有用的实践,技巧......

What are most important things you know about generics: hidden features, common mistakes, best and most useful practices, tips...

我开始实施的大部分使用泛型我的库/ API,并希望收集最常见的方式,技巧等,在实践中发现。

I am starting to implement most of my library/API using generics and would like to collect most common patterns, tips, etc., found in practice.

让我正式的问题:什么是最重要的事情,你已经了解泛型

Let me formalize the question: What is the most important thing you've learned about generics?

请尽量提供的例子 - 它会更容易理解,而不是令人费解的,过于干燥的描述

感谢

这问题有点类似<一个href="http://stackoverflow.com/questions/215548/whats-the-hardest-or-most-misunderstood-aspect-of-linq?...I">Jon's问题,不过,在不同的主题。

This question is somewhat similar to Jon's question, though, on a different subject.

推荐答案

一个我所学到的最重要的事情是,你可以的约束泛型类型参数(S)。这可能是非常强大的,让您充分利用自定义类只有某些类型的对象,并允许您使用该类型的成员在泛型类。我知道这是pretty的根本,但它是使得仿制药非常有用的。

One of the most important things I've learned is that you can constrain the generic type parameter(s). This can be very powerful, allowing you to take customize the class for only certain types of objects and allowing you to use the members of that type in your generic class. I realize that this is pretty fundamental, but it's one of the things that makes generics incredibly useful.

这篇关于关于C#泛型最重要的事情...教训的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆