Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Inbuilt Data Structures in C#
C# provides several powerful built-in data structures that make it easier to store, organize, and manipulate collections of data. These data structures are part of the .NET Framework and offer different capabilities for various programming scenarios.
The most commonly used built-in data structures include List<T> for dynamic arrays, ArrayList for non-generic collections, Dictionary<TKey, TValue> for key-value pairs, and Queue<T> and Stack<T> for specialized ordering operations.
List<T>
The generic List<T> is a strongly-typed collection that can dynamically resize itself. Unlike arrays, you don't need to specify the size at compile time, and it provides better type safety compared to non-generic collections.
Syntax
List<dataType> listName = new List<dataType>();
Example
using System;
using System.Collections.Generic;
class Program {
public static void Main() {
List<string> myList = new List<string>();
// Adding elements
myList.Add("Apple");
myList.Add("Banana");
myList.Add("Orange");
Console.WriteLine("List elements:");
foreach(string fruit in myList) {
Console.WriteLine(fruit);
}
Console.WriteLine("Count: " + myList.Count);
Console.WriteLine("Contains 'Apple': " + myList.Contains("Apple"));
}
}
The output of the above code is −
List elements: Apple Banana Orange Count: 3 Contains 'Apple': True
ArrayList
The ArrayList represents an ordered collection of objects that can be indexed individually. It is a non-generic collection that can store objects of any type, but requires boxing/unboxing for value types.
Syntax
ArrayList arrayListName = new ArrayList();
Example
using System;
using System.Collections;
class Program {
public static void Main() {
ArrayList arr = new ArrayList();
// Adding different types of elements
arr.Add(67);
arr.Add("Hello");
arr.Add(34.5);
arr.Add(true);
Console.WriteLine("ArrayList elements:");
foreach(object item in arr) {
Console.WriteLine(item + " (" + item.GetType().Name + ")");
}
Console.WriteLine("Count: " + arr.Count);
Console.WriteLine("Element at index 0: " + arr[0]);
}
}
The output of the above code is −
ArrayList elements: 67 (Int32) Hello (String) 34.5 (Double) True (Boolean) Count: 4 Element at index 0: 67
Dictionary<TKey, TValue>
The Dictionary<TKey, TValue> represents a collection of key-value pairs. It provides fast lookups based on keys and ensures that each key is unique.
Example
using System;
using System.Collections.Generic;
class Program {
public static void Main() {
Dictionary<string, int> ages = new Dictionary<string, int>();
// Adding key-value pairs
ages.Add("John", 25);
ages.Add("Sarah", 30);
ages.Add("Mike", 28);
Console.WriteLine("Dictionary contents:");
foreach(KeyValuePair<string, int> pair in ages) {
Console.WriteLine(pair.Key + ": " + pair.Value);
}
Console.WriteLine("John's age: " + ages["John"]);
Console.WriteLine("Contains key 'Sarah': " + ages.ContainsKey("Sarah"));
}
}
The output of the above code is −
Dictionary contents: John: 25 Sarah: 30 Mike: 28 John's age: 25 Contains key 'Sarah': True
Comparison of Built-in Data Structures
| Data Structure | Type Safety | Performance | Use Case |
|---|---|---|---|
| List<T> | Strong (Generic) | High | Dynamic arrays with known type |
| ArrayList | Weak (Non-generic) | Lower (boxing/unboxing) | Mixed-type collections (legacy) |
| Dictionary<TKey, TValue> | Strong (Generic) | High (O(1) lookup) | Key-value pair storage |
Conclusion
C# provides robust built-in data structures like List<T>, ArrayList, and Dictionary<TKey, TValue> for different collection needs. Generic collections like List<T> are generally preferred over non-generic ones like ArrayList due to better type safety and performance.
