how to use the foreach loop in a program to pass the values to an array in runtime
{
foreach(int k in n)
console.readline();
}
isn't this correct?
***Changed thread title***
-jjsaw5
foreach looppassing values to an array
Page 1 of 1
4 Replies - 1182 Views - Last Post: 31 March 2009 - 09:08 PM
Replies To: foreach loop
#2
Re: foreach loop
Posted 26 November 2008 - 06:14 AM
no here is an example of a foreach
output will be
Test1
Test2
Test3
Test4
ArrayList array = new ArrayList();
array.Add("Test1");
array.Add("Test2");
array.Add("Test3");
array.Add("Test4");
foreach(string value in array)
{
Console.WriteLine(value);
}
output will be
Test1
Test2
Test3
Test4
This post has been edited by zakary: 26 November 2008 - 06:15 AM
#3
Re: foreach loop
Posted 26 November 2008 - 08:16 AM
I would use a ForEach loop for this ONLY if it was absolutely required. It is much easier to write values into an array using a For loop:
string[] array = new string[5];
for (int i = 0; i < array.Length; i++)
{
array[i] = Console.ReadLine();
}
//using a ForEach to print them out is fine
foreach (string s in array)
{
Console.WriteLine(s);
}
Console.ReadLine();
#4
Re: foreach loop
Posted 26 November 2008 - 08:19 AM
Please use more descriptive and helpful thread titles. C# is not what we consider a good thread title.
#5
Re: foreach loop
Posted 31 March 2009 - 09:08 PM
foreach (string day in week)
{
MessageBox.Show("The day is : " + day);
}
http://csharp.net-in...oreach-loop.htm
tks.
{
MessageBox.Show("The day is : " + day);
}
http://csharp.net-in...oreach-loop.htm
tks.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|