xxxxxxxxxx
string[] myArray = {"Dogs", "Cats", "Birds"};
foreach (string item in myArray)
{
Console.WriteLine(item);
}
xxxxxxxxxx
int[] arr = new int[9]; //initialize the array, now it's [null,null,null,...];
for (int i = 0; i < arr.Length; i++)
{
Random rnd = new Random();
arr[i] =rnd.Next(0, 1001);
}
for (int i = 0; i < arr.Length; i++) // print a random arr, ^
{
Console.Write(", "+ arr[i]);
}
xxxxxxxxxx
//array of numbers, it works with strings also
int numbers[3] = {1, 4, 5, 6}
/*declares int named 'number' and assigns it the value of an index
starting from index 0 to the end of the array, in this case, 3*/
foreach(int number in numbers){
//prints the number
Console.WriteLine(number);
}
xxxxxxxxxx
foreach(var item in yourArray)
{
Console.WriteLine(item.ToString());
}
xxxxxxxxxx
using System;
namespace print_string_array
{
class Program
{
static void Main(string[] args)
{
string[] arr = new string[] { "one", "two", "three", "four" };
Console.WriteLine(String.Join("\n", arr));
}
}
}
xxxxxxxxxx
// Sample program to print an array in C#
using System;
class Program
{
static void Main(string[] args)
{
// Create an array with some elements
int[] numbers = { 2, 4, 6, 8, 10 };
// Print each element of the array
foreach (int num in numbers)
{
Console.WriteLine(num);
}
}
}