泛型的过度使用 [英] Overuse of Generics
问题描述
什么人认为仿制药的使用情况时,没有真正的好处是apparant。下面是code为例片段,它类似于code,我的工作,我试图确定什么好处,如果任何从使用泛型的上涨。
what do people think of the usage of Generics when no real benefit is apparant. Below is an example snippet of code which is similar to code I work on and I am trying to determine what benefit if any is gained from the use of generics.
public void DoSomething<Request>(Request request)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
if (Request is ISomeInterface)
{
DoSomething();
}
else
{
DoSomethingElse();
}
}
为什么你不直接使用对象?
Why would you not just use Object?
推荐答案
使用对象==投业务和拳击/ value-type的拆箱。 由于这些原因,仿制药的速度更快,更优雅(无 铸件)。而且 - 最主要的原因,你不会得到InvalidCastException的 使用泛型
Using object == cast operations and boxing/unboxing of value-types. Because of these reasons generics are faster and more elegant (no casting). And - the main reason, you won't get "InvalidCastException" using generics
因此,仿制药==速度和编译时错误。对象==运行 例外,较低的速度(有时要低得多)
So, generics == speed and compile-time errors. Object == runtime exceptions,lower speed (sometimes MUCH lower)
这篇关于泛型的过度使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!