Collections in C# are essential for storing and managing groups of objects, with various types including arrays, lists, dictionaries, queues, and stacks. Each collection type serves specific purposes and offers different functionalities, such as dynamic sizing in lists and key-value storage in dictionaries. Understanding these collections and their use cases is crucial for writing efficient and optimized code.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
19 views16 pages
Collections in CSharp With Examples
Collections in C# are essential for storing and managing groups of objects, with various types including arrays, lists, dictionaries, queues, and stacks. Each collection type serves specific purposes and offers different functionalities, such as dynamic sizing in lists and key-value storage in dictionaries. Understanding these collections and their use cases is crucial for writing efficient and optimized code.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16
Collections in C#
Overview with Examples
Introduction to Collections in C# • • Collections in C# are used to store, manage, and manipulate groups of objects. • • Collections come in different types depending on the use-case, such as arrays, lists, dictionaries, etc. • • They provide flexibility, allowing developers to dynamically manage data. Types of Collections • 1. Arrays • 2. Lists • 3. Dictionaries • 4. Queues • 5. Stacks Arrays in C# • • Arrays are a collection of data items of the same type. • • They have a fixed size, declared at the time of creation. • • Example: • int[] numbers = { 1, 2, 3, 4, 5 }; Lists in C# • • Lists are dynamic arrays that can grow or shrink in size. • • They are part of the System.Collections.Generic namespace. • • Example: • List<int> numbers = new List<int>() { 1, 2, 3, 4, 5 }; Dictionaries in C# • • Dictionaries store key-value pairs. • • Each key must be unique, and keys are used to access values. • • Example: • Dictionary<int, string> students = new Dictionary<int, string>(); • students.Add(1, "John"); Queues in C# • • Queues follow the FIFO (First In First Out) principle. • • Elements are added at the end and removed from the front. • • Example: • Queue<string> queue = new Queue<string>(); • queue.Enqueue("Task1"); Stacks in C# • • Stacks follow the LIFO (Last In First Out) principle. • • Elements are added and removed from the same end. • • Example: • Stack<string> stack = new Stack<string>(); • stack.Push("Plate1"); Generic vs Non-Generic Collections • • Generic Collections: • - Type-safe collections that store specific types (e.g., List<T>, Dictionary<TKey, TValue>). • • Non-Generic Collections: • - Collections that store objects without type safety (e.g., ArrayList, Hashtable). ArrayList Example • • ArrayList is a non-generic collection, can store any type of data. • • Example: • ArrayList list = new ArrayList(); • list.Add(10); • list.Add("Hello"); List Example • • Dynamic collection of specific type elements. • • Example: • List<int> numbers = new List<int> { 1, 2, 3 }; Dictionary Example • • Stores key-value pairs. • • Example: • Dictionary<int, string> dict = new Dictionary<int, string> { {1, "One"} }; Queue Example • • First In First Out collection. • • Example: • Queue<string> tasks = new Queue<string>(); • tasks.Enqueue("Task1"); Stack Example • • Last In First Out collection. • • Example: • Stack<int> stack = new Stack<int>(); • stack.Push(1); LINQ with Collections • • Language Integrated Query (LINQ) can be used to query collections. • • Example: • var results = numbers.Where(n => n > 2); Conclusion • • Collections in C# provide flexibility in storing and manipulating data. • • Different collections serve different purposes based on requirements. • • Understanding the strengths of each type helps in writing optimized code.