Results 1 to 6 of 6

Thread: Foreach statement of C sharp

  1. #1
    Join Date
    Dec 2009
    Posts
    38

    Foreach statement of C sharp

    Hello Friend,

    I learning the the C sharp programming language since two month. I have one query to ask you about the looping concept of C sharp. I am getting trouble while understanding the 'foreach' loop statement'. I am not able to find out the how the 'Foreach' statement differ from 'For' statement. If anybody of you aware about the foreach statement of C sharp, then please let know know that.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Foreach statement of C sharp

    Hi friend,

    The foreach statement of C sharp is identical to the 'for' statement> Bothe these statements are allows the program code for iteration on the program items. But eh there only difference is that the foreach statement of C sharp doesn't contains iteration index. So that the foreach statement can run even in the case of lack indices.The foreach statement of C sharp can iterates on the string array.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Foreach statement of C sharp

    See following example of Foreach statement of C sharp. In which the foreach loop iterates the string array of "Alpha1", "Bravo1", and "Charlie1":
    public class ForEachSample1
    {
    public void DoSomethingForEachItem1()
    {
    string[] itemsToWrite1 = {"Alpha1", "Bravo1", "Charlie1"};

    foreach (string item in itemsToWrite1)

    System.Console.WriteLine(item1);

    }
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Foreach statement of C sharp

    The foreach statement of C sharp is designed to cover the limitation of the 'for loop'. If the for statement's Boolean expression is not correct, the entire list will not be iterated.If the for statement's initialization variable is not initialized properly, the entire list will not be iterated.The for loop embedded statement needs to extract the element into a variable of the correct type.If the statement of for step is not correct, the entire list will not be iterated.Collections and arrays have different methods and properties for accessing their count.

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Foreach statement of C sharp

    The Foreach statement of C sharp is basically designed to designed to achieve the iteration of arrays. The type and identifier of a foreach statement declare the iteration variable of the statement.During execution of a foreach statement, the iteration variable represents the collection element for which an iteration is currently being performed.Using the foreach statement it is possible to avoid iteration problem by using the collection in uniform manner.

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Foreach statement of C sharp

    Hi,

    Please refer below example of Foreach statement of C sharp. Here I have used the foreach statement to count the values in the 'myValues1' array.
    public class Example12
    {

    public static void Main()
    {

    int [] myValues1 = {21, 41, 31, 51, 11};

    foreach (int counter1 in myValues1)
    {

    System.Console.WriteLine("counter1 = " + counter1);

    }

    }

    }

Similar Threads

  1. Sharp 007SH Hybrid An 3D Clamshell Android Phone by Sharp
    By jackalz in forum Web News & Trends
    Replies: 3
    Last Post: 22-05-2011, 07:28 AM
  2. which is better List<T>.ForEach or custom IEnumerable<T>?
    By MACE in forum Software Development
    Replies: 5
    Last Post: 19-02-2010, 09:56 PM
  3. lock Statement of C sharp
    By Sonam Goenka in forum Software Development
    Replies: 4
    Last Post: 02-02-2010, 02:07 PM
  4. Use of fixed statement in C sharp
    By Mithun Seth in forum Software Development
    Replies: 5
    Last Post: 02-02-2010, 01:27 PM
  5. Problem with a foreach loop in PHP
    By JiJi in forum Software Development
    Replies: 2
    Last Post: 20-11-2008, 05:57 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,351,626.51849 seconds with 16 queries