Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 19-01-2010
Member
 
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.
Reply With Quote
  #2  
Old 19-01-2010
Member
 
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.
Reply With Quote
  #3  
Old 19-01-2010
Member
 
Join Date: May 2008
Posts: 2,008
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":
Quote:
public class ForEachSample1
{
public void DoSomethingForEachItem1()
{
string[] itemsToWrite1 = {"Alpha1", "Bravo1", "Charlie1"};

foreach (string item in itemsToWrite1)

System.Console.WriteLine(item1);

}
}
Reply With Quote
  #4  
Old 19-01-2010
Member
 
Join Date: Apr 2008
Posts: 2,001
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.
Reply With Quote
  #5  
Old 19-01-2010
Member
 
Join Date: May 2008
Posts: 2,293
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.
Reply With Quote
  #6  
Old 19-01-2010
Member
 
Join Date: Oct 2005
Posts: 2,389
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.
Quote:
public class Example12
{

public static void Main()
{

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

foreach (int counter1 in myValues1)
{

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

}

}

}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 05:30 AM.