Foreach Loop In C#

The for-each loop in C# is developed to streamline the process of iterating over the elements of a collection, like arrays, lists, dictionaries, and other data structures that implement the IEnumerable or IEnumerable interface. It provides a convenient and effective method to access and handle each item in the collection. To grasp the concept behind the for-each loop in C#, take into account the subsequent essential points:

Enumeration: Fundamentally, the foreach loop operates as an iterator-driven iteration mechanism. It employs an enumerator, an entity responsible for maintaining the current location within the collection and facilitating the retrieval of elements sequentially. The enumerator is acquired from the collection through the GetEnumerator method.

The for-each loop operates in read-only mode, allowing read-only access to the elements within the collection. It enables retrieval and examination of the elements but prohibits direct modification within the loop. Trying to alter elements will lead to a compilation error.

Automatic Initialization and Termination: The for-each loop automatically initializes the enumerator, iterates through the collection, and terminates resources when the loop is completed or when an exception arises. This automated handling streamlines the iteration process and minimizes the chances of resource leaks.

The var keyword is commonly utilized in the foreach loop declaration to enable the compiler to infer the type of the current element.

Syntax:

It has the following syntax:

Example

for?ach (var it?m in coll?ction)
{
    // Cod? to b? ?x?cut?d for ?ach it?m in th? coll?ction
}

for?ach: It is th? k?yword that starts th? loop.

It's the declaration of a local variable (item in this case) representing the current element in the collection during each iteration. The keyword var is employed for implicit typing, enabling C# to deduce the data type of item based on the type of the elements in the collection.

It specifies the collection over which you wish to iterate. The collection must be an object that implements the IEnumerable or IEnumerable interface, which encompasses various collection types in C#, including arrays, lists, dictionaries, and more.

The code block enclosed in curly braces {} serves as the body of the foreach loop, where you insert the code that you wish to execute for each element in the collection.

Program: Working with Generics

Example

using Syst?m;
using Syst?m.Coll?ctions.G?n?ric;
class Program
{
    class Stud?nt
    {
        public string Nam? { g?t; s?t; }
        public List<int> Grad?s { g?t; s?t; }
        public Stud?nt(string nam?)
        {
            Nam? = nam?;
            Grad?s = n?w List<int>();
        }
        public void AddGrad?(int grad?)
        {
            Grad?s.Add(grad?);
        }
        public doubl? G?tAv?rag?Grad?()
        {
            if (Grad?s.Count == 0)
            {
                r?turn 0.0;
            }
            int sum = 0;
            for?ach (int grad? in Grad?s)
            {
                sum += grad?;
            }
            r?turn (doubl?)sum / Grad?s.Count;
        }
    }
    static void Main()
    {
        List<Stud?nt> stud?nts = n?w List<Stud?nt>
        {
            n?w Stud?nt("Alic?"),
            n?w Stud?nt("Bob"),
            n?w Stud?nt("Charli?")
        };
        for?ach (var stud?nt in stud?nts)
        {
            Consol?.Writ?Lin?($"Stud?nt: {stud?nt.Nam?}");
            Consol?.Writ?("Ent?r grad?s (comma-s?parat?d): ");
            string[] grad?StrArray = Consol?.R?adLin?().Split(',');
            for?ach (string grad?Str in grad?StrArray)
            {
                if (int.TryPars?(grad?Str, out int grad?))
                {
                    stud?nt.AddGrad?(grad?);
                }
            }
            doubl? av?rag?Grad? = stud?nt.G?tAv?rag?Grad?();
            Consol?.Writ?Lin?($"Av?rag? Grad?: {av?rag?Grad?:F2}");
            Consol?.Writ?Lin?();
        }
    }
}

Output:

Output

Stud?nt: Alic?
Ent?r grad?s (comma-s?parat?d): 2,3
Av?rag? Grad?: 2.50
Stud?nt: Bob
Ent?r grad?s (comma-s?parat?d):6,8
Av?rag? Grad?: 8.00
Stud?nt: Charli?
Ent?r grad?s (comma-s?parat?d): 5,6,8
Av?rag? Grad?: 6.33

Explanation:

For?ach Loop (Out?r Loop):

The outer for-each loop iterates through each student in the students list.

For every student, it carries out the subsequent actions:

Stud?nt Information Input:

  • It displays th? stud?nt's nam?.
  • Prompts th? us?r to ?nt?r grad?s for th? stud?nt (comma-s?parat?d).
  • R?ads th? ?nt?r?d grad?s as a string and splits it using Split(',') to obtain an array of strings.

Inn?r For?ach Loop (Grad? Input):

  • Th? inn?r for?ach loop it?rat?s through ?ach grad? string ?nt?r?d by th? us?r.
  • It tri?s to pars? ?ach grad? string to an int?g?r using int.TryPars?.
  • If th? parsing is successful, it adds th? pars?d grad? to th? stud?nt's list of grad?s using th? AddGrad? m?thod .

Average Grade Calculation:

Upon inputting all grades for the student, the average grade is computed utilizing the GetAverageGrade method.

Subsequently, the average grade is presented to the user with two decimal places ({averageGrade:F2}).

R?p?at for Each Stud?nt:

The forEach iteration persists until all students in the array have been processed.

Compl?xity Analysis:

Tim? Compl?xity:

  • Th? tim? compl?xity of cr?ating a list of n stud?nts is O(n) , wh?r? n is th? numb?r of stud?nts.
  • Th? out?r for?ach loop it?rat?s through ?ach stud?nt in th? stud?nts list. Sinc? th?r? ar? n stud?nts, th? tim? compl?xity of this loop is O(n) .
  • Th? inn?r for?ach loop it?rat?s through th? ?nt?r?d grad?s for ?ach stud?nt. In th? worst cas?, if a stud?nt has m grad?s , th? tim? compl?xity of this loop within th? out?r loop b?com?s O(m) , wh?r? m r?pr?s?nts th? maximum numb?r of grad?s among all stud?nts.
  • Calculating th? av?rag? grad? for ?ach stud?nt involv?s it?rating through th?ir grad?s. If a student has m grad?s, this calculation has a tim? compl?xity of O(m) .
  • Th? tim? compl?xity of th? ?ntir? program can b? ?xpr?ss?d as O(n * m) , wh?r? n is th? numb?r of stud?nts, and m is th? maximum numb?r of grad?s among all stud?nts.

Spac? Compl?xity:

  • Th? spac? compl?xity of th? stud?nts list is O(n) , wh?r? n is th? numb?r of stud?nts.
  • Th? program cr?at?s n instanc?s of th? Stud?nt class , wh?r? ?ach instanc? holds th? stud?nt's nam?, a list of grad?s, and som? additional ov?rh?ad for obj?ct r?f?r?nc?s. Th? spac? compl?xity for th?s? obj?cts is O(n) .
  • Each student has a list of grades. Th? spac? compl?xity for all grad? lists is O(m) , wh?r? m is th? maximum numb?r of grad?s among all stud?nts.
  • Th? program us?s som? t?mporary variabl?s lik? av?rag?Grad?, grad?StrArray , and grad? . Th? spac? compl?xity for th?s? variabl?s is r?lativ?ly small and can b? consid?r?d O(1).
  • Th? spac? compl?xity of th? program can b? approximat?d as O(n + m) , wh?r? n is th? numb?r of stud?nts, and m is th? maximum numb?r of grad?s among all stud?nts.

Program: working with lists

Example

using Syst?m;
using Syst?m.Coll?ctions.G?n?ric;
class Program
 {
    static void Main()
    {
        // Cr?at? a list of strings
        List<string> words = n?w List<string>
        {
            "appl?",
            "banana",
            "ch?rry",
            "dat?",
            "fig"
        };
        // Initializ? variabl?s to stor? th? long?st and short?st strings
        string long?st = "";
        string short?st = words[0]; // Assum? th? first word is th? short?st initially
        // Us? a for?ach loop to find th? long?st and short?st strings
        for?ach (string word in words)
        {
            if (word.L?ngth > long?st.L?ngth)
            {
                long?st = word;
            }
            if (word.L?ngth < short?st.L?ngth)
            {
                short?st = word;
            }
        }
        // Print th? r?sults
        Consol?.Writ?Lin?($"Long?st word: {long?st}");
        Consol?.Writ?Lin?($"Short?st word: {short?st}");
    }
}

Output:

Output

Long?st word: banana
Short?st word: fig

Explanation:

  • The program starts by creating a list of strings called words . This list contains five words: "appl?," "banana," "ch?rry," "dat?," and "fig" .
  • Two string variabl?s, long?st and short?st , ar? initializ?d. long?st, will stor? th? long?st word found, whil? short?st initially assum?s th? first word in th? list is th? short?st.
  • Th? program us?s a for?ach loop to it?rat? through ?ach word in th? words list. For ?ach it?ration , th? curr?nt word is r?pr?s?nt?d by th? variabl? word.
  • Insid? th? loop, th? program compar?s th? l?ngth of ?ach word to th? l?ngths of th? long?st and short?st words found so far. If th? curr?nt word is long?r than th? curr?nt long?st, long?st is updat?d. Similarly, if th? curr?nt word is short?r than th? curr?nt short?st, short?st is updat?d.
  • Aft?r th? for?ach loop finish?s, th? program prints th? long?st and short?st words to th? consol?.
  • Compl?xity Analysis:

Tim? Compl?xity:

  • Th? tim? compl?xity of cr?ating a list of n strings is O(n) , wh?r? n is th? numb?r of words in th? list.
  • Initializing th? long?st and short?st variabl?s tak?s constant tim?, O(1) .
  • Th? for?ach loop it?rat?s through ?ach word in th? words list onc?. Sinc? th?r? ar? n words, th? tim? compl?xity of this loop is O(n) .
  • Insid? th? loop, th? program compar?s th? l?ngth of ?ach word. Th?s? comparisons ar? constant tim? op?rations, O(1) . Printing th? r?sults involv?s constant tim? op?rations, O(1) .
  • Th? tim? compl?xity of th? ?ntir? program is dominat?d by th? for?ach loop, making it O(n) , wh?r? n is th? numb?r of words in th? list.

Spac? Compl?xity:

  • Th? spac? compl?xity of th? words list is O(n) , wh?r? n is th? numb?r of words in th? list.
  • Th? spac? compl?xity for th?s? variabl?s is O(1) , as th?y only hold r?f?r?nc?s to strings and do not d?p?nd on th? siz? of th? input.
  • Th? program us?s a small numb?r of t?mporary variabl?s, such as word , which hav? a constant spac? compl?xity, O(1) .
  • The spac? compl?xity of th? program is O(n) , primarily du? to th? spac? r?quir?d to stor? th? list of words. Th? oth?r variabl?s and t?mporary storag? do not significantly aff?ct th? ov?rall spac? compl?xity.

Program: Enum?rating Dictionari?s

Example

using Syst?m;
using Syst?m.Coll?ctions.G?n?ric;
class Program
{
    static void Main()
    {
        // Cr?at? a dictionary with k?y-valu? pairs
        Dictionary<string, int> ag?s = n?w Dictionary<string, int>
        {
            { "Alic?", 25 },
            { "Bob", 30 },
            { "Charli?", 28 }
        };
        // Us? a for?ach loop to ?num?rat? th? dictionary
        for?ach (var pair in ag?s)
        {
            string nam? = pair.K?y;
            int ag? = pair.Valu?;
            Consol?.Writ?Lin?($"{nam?}: {ag?} y?ars old");
        }
    }
}

Output:

Output

Alic?: 25 y?ars old
Bob: 30 y?ars old
Charli?: 28 y?ars old

Explanation:

  • The program starts by creating a Dictionary called ag?s . This dictionary is d?sign?d to stor? k?y-valu? pairs wh?r? th? k?ys ar? strings (r?pr?s?nting nam?s) and th? valu?s ar? int?g?rs (r?pr?s?nting ag?s).
  • Thr?? k?y-valu? pairs ar? add?d to th? ag?s dictionary using th? curly brac?s {} syntax within th? Dictionary initializ?r .
  • The main part of th? program is th? for?ach loop that it?rat?s through th? ag?s dictionary.
  • The for?ach loop is s?t up as follows: for?ach (var pair in ag?s).
  • In ?ach it?ration of th? loop, th? loop variabl? pair r?pr?s?nts a k?y-valu? pair from th? ag?s dictionary.
  • Th? loop continu?s to ?x?cut? until it has gon? through all th? k?y-valu? pairs in th? dictionary.
  • Insid? th? loop, w? ?xtract th? k?y (nam?) and valu? (ag?) from th? pair variabl?.
  • K?y r?tri?v?s th? k?y (nam?) , which is a string, and assigns it to th? nam? variabl?.
  • Valu? r?tri?v?s th? valu? (ag?) , which is an int?g?r, and assigns it to th? ag? variabl?.
  • Aft?r ?xtracting th? k?y (nam?) and valu? (ag?) , th? program us?s Consol?.Writ?Lin? to display this information in a human-r?adabl? format.
  • It prints th? nam? and ag? of ?v?ryon? in th? format "Nam?: Ag? y?ars old" using th? nam? and ag? variabl?s.
  • Th? for?ach loop r?p?ats this proc?ss for ?ach k?y-valu? pair in th? ag?s dictionary.
  • In this ?xampl?, it will it?rat? thr?? tim?s (onc? for ?ach p?rson in th? dictionary).
  • Compl?xity Analysis:

Tim? Compl?xity:

  • Th? tim? compl?xity of initializing a dictionary with n k?y-valu? pairs is O(n) , wh?r? n is th? numb?r of k?y-valu? pairs.
  • Th? for?ach loop it?rat?s through all th? k?y-valu? pairs in th? dictionary onc?. Sinc? th?r? ar? n k?y-valu? pairs, th? tim? compl?xity of this loop is O(n) .
  • Acc?ssing th? k?y and valu? of ?ach k?y-valu? pair using K?y and pair.Valu? op?rations is a constant-tim? op?ration, O(1) , because it doesn't d?p?nd on th? siz? of th? input.
  • Th? Consol?.Writ?Lin? op?ration for ?ach k?y-valu? pair is also a constant-tim? op?ration, O(1) .
  • Th? tim? compl?xity of th? ?ntir? program is dominat?d by th? for?ach loop, making it O(n) , wh?r? n is th? numb?r of k?y-valu? pairs in th? dictionary.

Spac? Compl?xity:

  • Th? spac? compl?xity of th? dictionary its?lf is O(n) b?caus? it stor?s n k?y-valu? pairs.
  • Th? spac? compl?xity for th?s? variabl?s is O(1) b?caus? th?y only hold r?f?r?nc?s to ?xisting data, and th? amount of m?mory th?y consum? do?s not d?p?nd on th? siz? of th? input.
  • Th? program prints strings to th? consol?, which r?quir?s t?mporary m?mory for th?s? strings. How?v?r, th? spac? us?d by th?s? strings is r?lativ?ly small and can b? consid?r?d O(1) .
  • Th? spac? compl?xity of th? program can b? approximat?d as O(n) , primarily du? to th? spac? r?quir?d to stor? th? dictionary. Th? oth?r variabl?s and t?mporary storag? do not significantly aff?ct th? ov?rall spac? compl?xity.
  • B?n?fits:

The for-each loop in C# provides numerous advantages, establishing it as the preferred option for iterating over collections, arrays, and enumerable data structures. Here are the key benefits of utilizing the for-each loop:

Optimization: Contemporary C# compilers and runtime environments frequently optimize foreach loops for enhanced performance. This enhancement can result in quicker code execution when compared to manually handling indices or iterators.

Reduced Code Boilerplate: When comparing to conventional for loops, the foreach loop removes the necessity for manual handling of loop variables, incrementing, and bounds checking. It decreases code verbosity and boilerplate.

Improved Error Handling: The foreach loop can assist in enhancing error handling. By abstracting the iteration logic, it minimizes the likelihood of off-by-one errors and index-related bugs that are prevalent in other loop constructs.

Improved Code Maintainability: The succinct and expressive quality of for-each loops simplifies the codebase for easier maintenance. Programmers invest less time troubleshooting iteration-related issues, enabling them to concentrate on higher-level logic.

Functional Programming Support: The for?ach loops adhere to the principles of functional programming by advocating a declarative approach to coding. This can result in cleaner and more maintainable code that prioritizes the what needs to be done over how it should be done.

Input Required

This code uses input(). Please provide values below: