Storing data with collections

If you need to store multiple values, you can use a collection. A collection is a data structure in memory that can manage multiple items in different ways, although all collections have some shared functionality.

There are two main namespaces for collections:

  • System.Collections: Avoid the types in this namespace because they store any type that derives from System.Object, that is, every type (which is flexible but not type safe, thus leading to errors and boxing of value types, which can be slow and inefficient).
  • System.Collections.Generic: The types in this namespace were introduced in C# 2 with .NET 2.0 and are better because they allow you to specify the type you want to store (which is safer, faster, and more efficient). ...

Get C# 6 and .NET Core 1.0: Modern Cross-Platform Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.