News:

Choose a design and let our professionals help you build a successful website   - ITAcumens

Main Menu

Example For How to use Array Program

Started by thiruvasagamani, Jul 11, 2008, 02:24 PM

Previous topic - Next topic

thiruvasagamani

This sample code listed in Listing 1 shows you how to use arrays. You can access an array items by using for loop but using foreach loop is easy to use and better.

Listing 1. Using arrays in C#.

using System;
namespace ArraysSamp
{
class Class1
{
static void Main(string[] args)
{
int[] intArray = new int[3];
intArray[0] = 3;
intArray[1] = 6;
intArray[2] = 9;
Console.WriteLine("================");
foreach (int i in intArray)
{
Console.WriteLine(i.ToString() );
}
string[] strArray = new string[5]
{"Ronnie", "Jack", "Lori", "Max", "Tricky"};
Console.WriteLine("================");
foreach( string str in strArray)
{
Console.WriteLine(str);
}
Console.WriteLine("================");
string[,] names = new string[,]
{
{"Rosy","Amy"},
{"Peter","Albert"}
};
foreach( string str in names)
{
Console.WriteLine(str);
}
Console.ReadLine();
}
}
}

The output of Listing 1 looks like below

Thiruvasakamani Karnan


karthikn

Thank you for your kind Information Thiru  :educated