Iterating Collections in c#

Iterating Collections in c#

In the C# programming language, C# programmers can iterate over List<T>, Stack<T>, Queue<T>, Dictionary<TKey, TValue>, and other collection data types by applying multiple individual program loop constructs and methods. There are several methods for iterating over collection data types in general, including using foreach, for, or while loops, as well as using LINQ data connected methods for more complex program queries.

Iterating Collections in c#

So, let’s learn how to iterate over collections List<T>, Stack<T>, Queue<T>, and Dictionary<TKey, TValue> in C# programming.

Iterating over the List<T> collection data type.

List<T> is a common collection data type concept in C# programs, which C# users can iterate over using foreach loops, for loops, or LINQ methods.

Using a foreach loop in the List<T> data type.

Here in C# user program, foreach loop can use this loop to iterate in List<T>.

Foreach loop example in the List<T>.

Using System;

using System.Collections.Generic;

class ListIterate

{

static void Main()

{

List<int> integers = new List<int> { 8, 9, 1, 7, 3, 2, 10 };

// here we Iterating list data type using foreach loop

foreach (int integers in integers)

{

Console.WriteLine(intgrs); // Result is – 8 9 1 7 3 2 10

}

}

}

Using a for loop in a list collection data type.

When a C# user needs to access the index location of each element in a program, the for loop is an excellent method for iterating through list data from start to end.

For loop example in the List<T>.

using System;

using System.Collections.Generic;

class ListIForIterate

{

static void Main()

{

List<string> employees = new List<string> { “Harry”, “Siddhi”, “Bhavishi”, “Amit”};

// here iterates using the for loop (accessing the index)

for (int p = 0; p < employees.Count; p++)

{

Console.WriteLine(employees[p]); // Result is – Harry”, Siddhi, Bhavishi, Amit

}

}

}

}

Using LINQ with the collection data type.

C# users can also use LINQ (Language Integrated Query) to apply multiple loop programming operations to collection data types. For example, it can be used to loop through a list data type element and select only a few particular elements.

LINQ with the collection data type example.

using System;

using System.Collections.Generic;

using System.Linq;

class LinqIterate

{

static void Main()

{

List<int> integers = new List<int> { 21, 28, 33, 44, 57, 88, 91, 100 };

// Here we are using the LINQ method to filter even integers into numeric ones and print them to the console screen.

var evenIntegers = integers.Where(n => n % 2 == 0);

foreach (var intgrs in evenIntegers)

{

Console.WriteLine(intgrs); // Result is – 28 44 88 100

}

}

}

Iterating over the Stack<T> collection data type.

In C# programming, a Stack<T> collection data type follows the last-in, first-out (LIFO) order. Therefore, C# users can loop or iterate over it just like the List<T> data type. Since the stack data type is in LIFO order, iterating directly will not maintain the order in which stack items are added. C# users can still use a foreach loop to preview the data elements stored in the stack. Here, the stack will be ordered from the most recently added stack item to the least recently added item.

Example of a foreach loop in a Stack<T> collection data.

using System;

using System.Collections.Generic;

class StackIterate

{

static void Main()

{

Stack<string> stack = new Stack<string>();

stack.Push(“C#”);

stack.Push(“Java”);

stack.Push(“Python”);

stack.Push(“Matlab”);

stack.Push(“MySql”);

// Here iterates the stack element using a foreach loop

foreach (var element in stack)

{

Console.WriteLine(element); // Result is – MySQL, Matlab, Python, Java, C# (LIFO stack element order)

}

}

}

Using the Stack<T> collection data type for a while loop.

If a C# user needs to process or retrieve the Stack collection data type elements in the same order in which they were added to the stack, e.g., data elements from the stack To pop, C# users can apply a while loop to dequeue all stack data elements.

Stack<T> collection data type example with a while loop.

using System;

using System.Collections.Generic;

class StackWhileIterate

{

static void Main()

{

Stack<int> stack = new Stack<int>();

stack.Push(9);

stack.Push(8);

stack.Push(7);

stack.Push(6);

stack.Push(5);

// Here it iterates using a while loop to pop stack element items

while (stack.Count > 0)

{

Console.WriteLine(stack.Pop()); // Result is – 5 6 7 8 9 (LIFO stack element order)

}

}

}

Iterating the Queue<T> collection data type.

In C# programming, a Queue<T> collection data type follows the First In, First Out (FIFO) concept or order. The Queue<T> data type is typically iterated in the same order in which new data items were added to the Queue<T> data type.

Queue<T> collection data type foreach loop example.

using System;

using System.Collections.Generic;

class QueueIterate

{

static void Main()

{

Queue<string> queue = new Queue<string>();

queue.Enqueue(“MBA”);

queue.Enqueue(“BBA”);

queue.Enqueue(“BCA”);

queue.Enqueue(“MCA”);

queue.Enqueue(“MTECH”);

// Here iterates queue data using the foreach loop method

foreach (var element in queue)

{

Console.WriteLine(element); // Result is – MBA BBA BCA MCA MTECH (FIFO queue element order)

}

}

}

Using a while loop on the Queue<T> collection data type.

To dequeue the Queue collection data type elements and iterate through the queue elements in a loop, C# users can apply a while loop.

Queue<T> collection data type while loop example.

using System;

using System.Collections.Generic;

class QueueWhileIterate

{

static void Main()

{

Queue<int> queue = new Queue<int>();

queue.Enqueue(41);

queue.Enqueue(31);

queue.Enqueue(21);

queue.Enqueue(10);

queue.Enqueue(01);

// Here iterates using a while loop to dequeue queue element items

while (queue.Count > 0)

{

Console.WriteLine(queue.Dequeue()); // Result is – 41 31 21 10 1 (FIFO queue element order)

}

}

}

Iterating the Dictionary<TKey, TValue> collection data type.

In C# programming, a Dictionary<TKey, TValue> collection data type stores data and information in a key-value pair order, and C# users can iterate over both dictionary keys and values ​​using a loop.

Using a foreach loop in a Dictionary<TKey, TValue>.

C# users can apply a foreach loop to iterate over KeyValuePair<TKey, TValue> collection data element objects in a dictionary.

Example of a foreach loop in a Dictionary<TKey, TValue>.

Using System;

using System.Collections.Generic;

class DictionaryForeach

{

static void Main()

{

Dictionary<string, int> salary = new Dictionary<string, int>();

salary.Add(“Bhavshi Deora”, 77000);

salary.Add(“Siddhi Deora”, 67000);

salary.Add(“Harry Deora”, 99000);

salary.Add(“Vivek”, 100000);

// here it Iterating Dictionary element using foreach loop with Accessing keys and its values

foreach (var pair in salary)

{

Console.WriteLine($”\n Employee Name – {pair.Key} \n Salary – {pair.Value}”);

}

}

}

Using the Dictionary Keys and Values ​​Properties.

C# users can iterate over the keys or values ​​of a dictionary using the Dictionary Keys and Values ​​properties using a foreach loop.

Using the Dictionary Keys and Values ​​foreach Example.

using System;

using System.Collections.Generic;

class DictionaryKeyValues

{

static void Main()

{

Dictionary<string, int> salary = new Dictionary<string, int>();

salary.Add(“Bhavshi Deora”, 77000);

salary.Add(“Siddhi Deora”, 67000);

salary.Add(“Harry Deora”, 99000);

salary.Add(“Vivek”, 100000);

// here it Iterating dictionary element over keys

foreach (var key in salary.Keys)

{

Console.WriteLine(“Employee Name – ” + key);

}

//here it Iterating over values

foreach (var elements in salary.Values)

{

Console.WriteLine(“Salary – ” + elements);

}

}

}

Using LINQ methods to iterate over a Dictionary.

LINQ (Language Integrated Query) in C# programs provides a powerful method to query collection data types and iterate over data element objects through a loop. LINQ methods can be used with collection data types like List<T>, Queue<T>, Stack<T>, Dictionary<TKey, TValue>, etc. to easily loop through them.

Example of a LINQ method to filter and iterate.

using System;

using System.Collections.Generic;

using System.Linq;

class LinqMethodIterate

{

static void Main()

{

List<int> integers = new List<int> { 33, 12, 21, 24, 55, 66, 77, 100 };

// Here we use the LINQ method to filter even integers and iterate with a foreach loop

var evenIntegers = integers.Where(i => i % 2 == 0);

foreach (var intgrs in evenIntegers)

{

Console.WriteLine(intgrs); // Result is – 12 24 66 100

}

}

}

Detail Information About List<T>, Stack<T>, Queue<T>, Dictionary<TKey, TValue> Collections Data Iteration Methods.

Collection Data TypeCommon Data Iteration MethodAbout Information
List<T> data typeList data type use foreach, for loop for data value iterationWe can access list collection data type Access elements in any sequence or order.
Stack<T> data typeforeach, while loop used for (using Pop()) operation in stack elementStack data type use LIFO (Last In, First Out), order may be reversed element display pattern.
Queue<T> data typeforeach, while used in queue data type for (using Dequeue()) loop operationQueue data type used FIFO (First In, First Out), order is maintained to access its element.
Dictionary<TKey, TValue> data typeforeach, Keys, Values used in dictionary data type with start to endDictionary data type Access both keys and values or just keys/values in given object or element.
LINQ methodLinq method used to get data query with Where(), Select(), ForEach() loop or sql methodLinq method Efficient way to filter and transform group of collections data extraction.

List<T>, Stack<T>, Queue<T>, Dictionary<TKey, TValue> Collections Data Type Iteration Summary.

Iterating over collections data in C# data types can be done by directly applying various looping concept constructs like foreach, for, and while, or by applying LINQ concept methods for more advanced data element filtering and querying. Based on the different collection data types, C# users can choose the most appropriate method to iterate through the data through a loop and manipulate it efficiently.

Leave a Reply