Iterate through list in C Sharp(5 ways)

In this post, we are going to learn how to iterate through list in C sharp or print all elements of a list or print elements of the list based on condition using LINQ with examples. The list can be a simple list or list of a specific type.

1. Iterate through list in C sharp using string.join()


In the below example, we are using the string.join() method to iterate through the list. The string.join() method concatenates all elements of a given list separated by a separator, here we have used a comma(,), which can be changed as pe requirement

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpConsole
{
    class Program
    {


        static void Main(string[] args)
        {
            var days = new List<string>();
            days.Add("Mon");
            days.Add("Tues");
            days.Add("Wednes");
            days.Add("Thrus");

            Console.WriteLine(string.Join(", ", days));
            Console.ReadLine();


    }
    }
}

Output

Mon, Tues, Wednes, Thrus

2. Iterate through list in C sharp using for loop


In the below example, We have simply used the for loop to iterate over all elements of the list and print on the console. We have to start the loop from 0 till End the length of the given list. On each iteration, we have to print the element using the index. using days[i]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpConsole
{
    class Program
    {


        static void Main(string[] args)
        {
            var days = new List<string>();
            days.Add("Mon");
            days.Add("Tues");
            days.Add("Wednes");
            days.Add("Thrus");
            for (int i = 0; i < days.Count; i++)
            {
                Console.WriteLine(days[i]);
            }
            
            Console.ReadLine();


    }
    }
}

Output

Mon
Tues
Wednes
Thrus
        

3. Using foreach method to Iterate through list in C sharp


In this example, we have used the list foreach (action<T>) method to iterate through the list and print the result on the console.

var days= new List<string>();
days.Add("Mon");
days.Add("Tues");
days.Add("Wednes");
days.Add("Thrus");

days.ForEach(day => Console.WriteLine(day ));}

Output

Mon
Tues
Wednes
Thrus
        

4. Iterate through list in C sharp LINQ


In this example, we have used the LINQ style for-each loop to iterate through the list based on condition. First, iterating through each element of the list and checking the condition where the day is not equal to “Wednes” and finally printing the days on the console.

var days = new List<string>();
            days.Add("Mon");
            days.Add("Tues");
            days.Add("Wednes");
            days.Add("Thrus");
           
            days.Where(x => x!= "Wednes").ToList().ForEach(x => Console.WriteLine(x));
            Console.ReadLine();
        

Output

Mon
Tues
Thrus

5. Iterate through list in C sharp based on condition Using LINQ


In this example, we have two lists Weekdays, and WeekEnd. We are filtering the Weekday’s list, by checking if weekend list elements exist in Weekday’s list using LINQ and the contain() method

namespace CSharpConsole
{
    class Program
    {


        static void Main(string[] args)
        {
            var Weekdays = new List<string>() { "Mon", "Tues", "Wednes", "Thrus","Fri" };
            var WeekEnd = new List<string>() { "Sun", "Sat","Mon", "Wednes" };
            Weekdays.Where(x => WeekEnd.Contains(x))
    .ToList()
    .ForEach(x => Console.WriteLine(x));

            Console.ReadLine();
        

        


    }
    }
}

Output

Mon
Wednes