Should we use Generic Collection to improve safety and performance?

前端 未结 3 1649
滥情空心
滥情空心 2021-01-24 07:11

Should we use Generic Collection to improve safety and performance?

相关标签:
3条回答
  • 2021-01-24 07:19

    Of course. Why wouldn't you? More important than the performance, IMO, is the fact that generic APIs are more expressive. That goes for generic APIs in general, not just collections.

    EDIT: Just to clarify these a bit:

    • Performance characteristics are slightly different between the generic and nongeneric collections for reasons of "they're different implementation" as well as the whole generic/nongeneric side. Obviously the generic version avoids boxing/unboxing and (in most use cases) an execution time cast when fetching. In practice this is only likely to be significant for value types, where the boxing comes into play. For large collections, it's the difference in memory usage which is likely to be more significant than the execution speed difference.

    • I'm slightly less bothered by the actual type-safety aspect. It's certainly a good thing, but I can't remember ever actually seeing a bug when using the nongeneric collections due to putting the wrong type in (or fetching it as the wrong type). It's certainly a benefit though.

    • I view the expressiveness as very important. I can glance at the declaration of a method and know what to expect from the return value - I don't need to read the documentation quite as closely, or give variables quite as unwieldy names or docs. In addition you get all the benefits of Intellisense etc. One of the questions to ask about a language is "what can I express in it?" and generics allows much richer concepts to be expressed than before.

    0 讨论(0)
  • 2021-01-24 07:30

    Short answer: yes

    Longer answer: There are really no downsides to using generic collections. Compile-time type checking eliminates the posibility of runtime errors from casting. Performance will be greater for built-in types such as integers, since boxing and unboxing are not needed (in contrast to Java generic collections, by the way)

    0 讨论(0)
  • 2021-01-24 07:34

    Absolutely.

    A conventional collection, such as the ArrayList, implicitly stores objects.

    This means, that doing this:

    ArrayList list = new ArrayList();
    list.Add(5);
    list.Add("FooBar");
    

    Is legitimate code. This introduces a handful of issues.

    • Normally, you don't want to store different types in the same collection, and having compile time checking for this is nice.
    • When you store a value type (such as the integer 5 above), it must be boxed into a reference type before it can be stored in the collection.
    • When reading a value, you must cast it back from Object to the desired type.

    However, you eliminate all of these issues by using a generic collection:

    List<int> list = new List();
    list.Add(5);
    // Compile Time Error.
    list.Add("FooBar")
    

    You also gain intellisense support when working directly with indices of the collection, instead of just generic "object" intellisense.

    0 讨论(0)
提交回复
热议问题