top of page
Writer's picturehr9083

Collections in C#

In C#, collections are essential data structures designed to manage and manipulate groups of interconnected objects efficiently. They are part of the .NET Framework's base class library and provide various implementations for different needs. Here are some commonly used collections in C#: 




  • Arrays : Arrays are collections of a fixed size designed to contain elements of uniform type. Arrays offer fast access to elements by index but cannot be resized dynamically. 


  • Lists (List<T>) :Dynamic arrays that automatically resize themselves when needed. Lists provide fast insertion and removal of elements, as well as indexed access. 


  • Stacks (Stack<T>) : LIFO (Last In, First Out) collections that allow adding and removing elements from one end (the top). Useful for implementing algorithms such as undo functionality. 


  • Queues (Queue<T>) : FIFO (First In, First Out) collections that allow adding elements to one end (the rear) and removing elements from the other end (the front). They prove invaluable for implementing algorithms like breadth-first search. 


  • Dictionaries (Dictionary<TKey, TValue>) : Key-value pair collections that allow fast lookup of values based on keys. Keys within the dictionary must maintain uniqueness. 


  • Sets (HashSet<T>, SortedSet<T>) : Collections that store unique elements. Sets do not allow duplicate elements and provide operations such as union, intersection, and difference. 


  • LinkedLists (LinkedList<T>) : Doubly linked lists that allow efficient insertion and removal of elements at any position. LinkedLists provide iterators for sequential access. 


  • Queues (ConcurrentQueue<T>) and Stacks (ConcurrentStack<T>) in the System.Collections.Concurrent namespace : Thread-safe versions of queues and stacks that can be accessed by multiple threads concurrently. 


These collections provide different performance characteristics and features, allowing you to choose the most appropriate one for your specific use case. Additionally, C# provides LINQ (Language Integrated Query) extensions that enable querying and manipulating collections using a declarative syntax. 


Here are some examples: 


Example 1 : 



Code Explanation:


Namespace Import :

  • The code begins with two using directives: System and System.Collections.Generic. These directives allow the program to access classes and types defined in the specified namespaces without fully qualifying them.


Main Method :

  • he Main method is the program's entry point. It's a static method that doesn't return anything (void).


Main Method :

  • A list of integers named numbers is declared and initialized using a collection initializer. It encompasses the values 10, 20, and 30.


Accessing Elements :

  • The first element of the list (numbers[0]) is printed to the console using Console.WriteLine().


Iterating Through the List :

  • All elements of the list are printed to the console using the ForEach() method of the List<T> class. Console.WriteLine is passed as an argument to ForEach(), which means that each element will be printed on a new line.


Removing an Element :

  • The element 20 is removed from the list using the Remove() method of the List<T> class. After this line executes, the list will contain only the values 10 and 30.


Checking for an Element :

  • The code checks if the list contains the value 20 using the Contains() method of the List<T> class. Depending on the result, it prints either "20 is in the list." or "20 is not in the list." to the console using a ternary conditional operator (? :).


Example 1 : 


Output :


Fruits

Apple

Banana

Orange

Mango


Code Explanation :


Initialization :

  • A list of strings named fruits is created and initialized with initial values "Apple", "Banana", and "Orange" using a collection initializer.


Adding an Element :

  • The code adds a new fruit, "Mango", to the list using the Add() method. This expands the list to include the new element.


Printing Fruits :

  • The program prints the header "Fruits:" to indicate that it will display the list of fruits.

  • It then iterates through the list of the fruits using a for each loop.

  • During each iteration, it prints the current fruit to the console.

0 views0 comments

Recent Posts

See All

Comments


bottom of page