C# Fundamentals: Arrays and Collections
In C#, arrays and collections are fundamental data structures used to store and manage groups of items. Understanding them is crucial for efficient data handling, especially when working with .NET development and integrating with services like Azure.
Arrays: Fixed-Size Lists
Arrays are the most basic form of collection. They are fixed-size, meaning once an array is created, its size cannot be changed. Elements in an array are accessed using an index, which starts at 0.
Arrays store elements of the same data type in contiguous memory locations.
Arrays provide a way to group multiple values of the same type under a single variable name. Accessing elements is done via their numerical position (index).
When you declare an array, you specify the data type of its elements and its size. For example, int[] numbers = new int[5];
creates an array that can hold five integers. You can then assign values to each element: numbers[0] = 10; numbers[1] = 20;
and so on. The size of the array is fixed at creation. Iterating through an array is a common operation, often done using a for
loop.
Arrays in C# have a fixed size once they are declared and initialized.
Collections: Dynamic and Flexible
While arrays are useful, their fixed size can be a limitation. C# provides a rich set of collection classes in the
System.Collections.Generic
Feature | Array | Collection (e.g., List<T>) |
---|---|---|
Size | Fixed | Dynamic (can grow/shrink) |
Type Safety | Strongly typed (all elements same type) | Strongly typed (generic collections) |
Performance | Generally faster for direct access | Can have overhead but offers more operations |
Flexibility | Limited (cannot easily add/remove) | High (add, remove, sort, search) |
Common Generic Collections
The
System.Collections.Generic
- : A dynamic array that can grow or shrink as needed. It's one of the most versatile and commonly used collections.codeList
- : A collection of key-value pairs. Each key must be unique, and it's used to quickly retrieve a value associated with a specific key.codeDictionary
- : A collection that stores unique elements. It's optimized for checking if an element exists in the collection.codeHashSet
- : A First-In, First-Out (FIFO) collection. Elements are added to the end and removed from the beginning.codeQueue
- : A Last-In, First-Out (LIFO) collection. Elements are added to and removed from the top.codeStack
Visualizing the difference between a List and a Dictionary. A List is like a numbered shopping list where you can add or remove items easily. A Dictionary is like a phone book, where you look up a name (the key) to find a phone number (the value). The generic type parameters <T>
indicate that these collections can hold any data type, providing type safety.
Text-based content
Library pages focus on text content
HashSet<T>
Integration with Azure
Arrays and collections are fundamental when interacting with Azure services. For instance, when retrieving data from Azure Cosmos DB or Azure Blob Storage, you'll often receive results as collections or arrays of objects. Efficiently processing this data using C# collections is key to building performant cloud applications.
When working with Azure SDKs, you'll frequently encounter methods that return IEnumerable<T>
, ICollection<T>
, or List<T>
, making a solid understanding of these structures essential for data manipulation and processing.
For example, when fetching a list of blobs from Azure Blob Storage, the SDK might return a
List
List
Azure SDKs often return IEnumerable<T>
or List<T>
. Understanding collections is crucial for efficiently processing and manipulating the data retrieved from Azure services.
Learning Resources
Official Microsoft documentation providing a comprehensive overview of C# arrays, including declaration, initialization, and usage.
An in-depth guide to the various collection types available in C#, covering generic collections and their specific use cases.
Detailed API reference for the `List<T>` class, outlining its methods, properties, and common operations.
API documentation for the `Dictionary<TKey, TValue>` class, explaining how to work with key-value pairs.
A practical tutorial explaining the concept of generics in C# and how they are used with collections like List<T>.
A video tutorial demonstrating the practical use of various C# collections, including List, Dictionary, and more.
Introduction to Azure Blob Storage and its .NET SDK, which often returns data in collection formats.
Learn how to use LINQ (Language Integrated Query) to query and manipulate collections efficiently in C#.
A beginner-friendly explanation of fundamental data structures in C#, including arrays and lists.
A comprehensive overview of C# collections, detailing their types, advantages, and common operations.