i have a question regarding with Arrays:
I am wondering if your able to use a foreach in a multidimensional array. I am able to use a nested for loop to output the information. I am curious about it. I would greatly appreciate the help.
Array Problems
Started by jaffemutant, May 15 2010 09:39 AM
4 replies to this topic
#1
Posted 15 May 2010 - 09:39 AM
|
|
|
#2
Posted 15 May 2010 - 09:45 AM
What is the point in using a foreach if you have an array?
Just use two for's:
Just use two for's:
for (int i = 0; i<n; i++)
for (int j = 0; j<n; j++)
Console.WriteLine(array[i][j]);
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics
#3
Posted 15 May 2010 - 09:51 AM
I realized that using a foreach is only meant for a single dimensional array. But, I just wanted to make sure that you still can use it for multidimensional
#4
Posted 17 May 2010 - 05:21 AM
Davide said:
What is the point in using a foreach if you have an array?
when you use foreach code is clean. You dont have to declare additional indexing variables.
#5
Posted 17 May 2010 - 05:28 AM
Following code prodcues output: "1234".
Wthout any variable.
Wthout any variable.
class Program
{
static void Main(string[] args)
{
var array = new [,] {{1, 2}, {3, 4}};
foreach (var value in array)
Console.Write(value);
Console.Read();
}
}


Sign In
Create Account

Back to top









