When to use ArrayList over array[] in c#?

前端 未结 11 1730
别那么骄傲
别那么骄傲 2020-12-01 04:38

I often use an ArrayList instead of a \'normal\' array[].

I feel as if I am cheating (or being lazy) when I use an ArrayList,

相关标签:
11条回答
  • 2020-12-01 05:09

    The array's size is static, so if you do know the size at design time, use array. It is supposed to work faster, but I haven't tested it myself. If you need to change the count of objects frequently (adding or removing objects from the collection) use ArrayList or better the generic List from .NET 2. It's also easier to use, so if performance is not crucial, you can always use List.

    0 讨论(0)
  • 2020-12-01 05:10

    If you need an array of primitive types use Array for better performance as it will avoid autoboxing and unboxing. But only if you know the size you want ahead of time.

    0 讨论(0)
  • 2020-12-01 05:11

    Well for one, if you only intend to handle a specific type, you shouldn't use an ArrayList. For example, if you only expect an array of bytes, you should only accept an array of bytes.

    Only time I would think you may even think of using an ArrayList is instead of List.

    0 讨论(0)
  • 2020-12-01 05:13

    Arrays are strongly typed, and work well as parameters. If you know the length of your collection and it is fixed, you should use an array.

    ArrayLists are not strongly typed, every Insertion or Retrial will need a cast to get back to your original type. If you need a method to take a list of a specific type, ArrayLists fall short because you could pass in an ArrayList containing any type. ArrayLists use a dynamically expanding array internally, so there is also a hit to expand the size of the internal array when it hits its capacity.

    What you really want to use is a generic list like List<T>. This has all the advantages of Array and ArrayLists. It is strongly typed and it supports a variable length of items.

    0 讨论(0)
  • 2020-12-01 05:14

    I'm answering this from a Java perspective, but it's the same basic issue. You should not feel guilty using higher abstractions. After all, you are using Strings instead of char[], or even byte[]? I'd even suggest to go one step further and use the List interface where possible. The only reason to go one step down is for performance reasons.

    Using the higher collection abstraction has many advantages. You can add decorators to make the List read-only, make it fixed size, check items that enter or leave the collection or use views (see GetRange in C# and subList in Java.)

    By the way, an ArrayList should always be based on a primitive array, otherwise the name is wrong. The operations are usually implemented just how you would expect when using a primitive array. If a linked list is used it's usually named just that -- LinkedList. That's also an advantage of using the interface: You can change your mind about the used implementation later on.

    There are a few things that make the use of collections clunky. One caveat is that the collections are usually based on objects, and the languages have a considerable gap between primitive and object types. The limited generics don't help much either. Still, I recommend the collections over arrays unless there's a good reason otherwise.

    For primitive values you may also consider using a primitive collection library, for example GNU Trove. Don't know if there's anything similar for C#, though.

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