泛型的过度使用 [英] Overuse of Generics

查看:142
本文介绍了泛型的过度使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么人认为仿制药的使用情况时,没有真正的好处是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?

推荐答案

引用自: C#: System.Object的VS泛型

使用对象==投业务和拳击/ 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屋!

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