C#中的泛型是什么? [英] What are generics in C#?

查看:24
本文介绍了C#中的泛型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用一个简单的例子说明什么是 C# 中的泛型?有哪些与此主题相关的文章或网站?

What are generics in C#, illustrated with a simple example? What are some related articles or websites for this topic?

推荐答案

泛型是指为类编写代码而不指定类工作的数据类型的技术.

Generics refers to the technique of writing the code for a class without specifying the data type(s) that the class works on.

在声明泛型类的实例时指定数据类型.这允许泛型类专用于许多不同的数据类型,而只需编写一次类.

You specify the data type when you declare an instance of a generic class. This allows a generic class to be specialized for many different data types while only having to write the class once.

一个很好的例子是 .NET 中的许多集合类.每个集合类都有自己的集合创建和管理方式的实现.但是他们使用泛型来允许他们的类处理任何类型的集合.

A great example are the many collection classes in .NET. Each collection class has it's own implementation of how the collection is created and managed. But they use generics to allow their class to work with collections of any type.

http://msdn.microsoft.com/en-us/library/ms379564(VS.80).aspx

这篇关于C#中的泛型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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