什么是酷的仿制药,为什么使用它们? [英] What is cool about generics, why use them?

查看:141
本文介绍了什么是酷的仿制药,为什么使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我会提供这种垒球给任何人想打出来的公园中。什么是仿制药,什么是仿制药,所以,在这里,我应该如何使用它们的优势是什么?请保持相当基础。谢谢你。

I thought I'd offer this softball to whomever would like to hit it out of the park. What are generics, what are the advantages of generics, why, where, how should I use them? Please, keep it fairly basic. Thanks.

推荐答案


  • 允许你写code /使用库方法这是类型安全的,即一个List<串GT;保证是字符串列表。

  • 作为仿制药的结果所使用的编译器可以在code进行编译时检查类型安全,即你试图把一个int到字符串该名单?使用一个ArrayList会导致这是一个不太透明运行时错误。

  • 比使用对象,因为它要么避免装箱/拆箱(其中.NET有转换<更快href=\"http://stackoverflow.com/questions/5057267/what-is-the-difference-between-a-reference-type-and-value-type-in-c\">value类型引用类型或反之亦然),或从对象到所需的引用类型铸造。

  • 允许你写code这是适用于多种类型的相同的基本行为,即一个字典&LT;字符串,整数&GT;使用相同的基础code作为一个字典&LT;日期时间,双取代;使用泛型,框架团队只需编写一件code的实现与上述优点两个结果了。

  • Allows you to write code/use library methods which are type-safe, i.e. a List<string> is guaranteed to be a list of strings.
  • As a result of generics being used the compiler can perform compile-time checks on code for type safety, i.e. are you trying to put an int into that list of strings? Using an ArrayList would cause that to be a less transparent runtime error.
  • Faster than using objects as it either avoids boxing/unboxing (where .net has to convert value types to reference types or vice-versa) or casting from objects to the required reference type.
  • Allows you to write code which is applicable to many types with the same underlying behaviour, i.e. a Dictionary<string, int> uses the same underlying code as a Dictionary<DateTime, double>; using generics, the framework team only had to write one piece of code to achieve both results with the aforementioned advantages too.
  • 这篇关于什么是酷的仿制药,为什么使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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