How to use array in c# .Net?
Arrays is a collections of items, for instance strings.Arrays are declared much like variables, with a set of [] brackets after the datatype, like this:
string[] Mobile ;
string[] Mobile = new string[2];
The number (2) is the size of the array, that is, the amount of items we can put in it. Putting items into the array is pretty simple as well:
Mobile [0] = "Samsuang";
Mobile [1] = "sony",
Mobile [2] = "Nexus"
Array counting starts from 0 instead of 1. So the first item is indexed as 0, the next as 1 and so on.
Example
using System; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string[] Mobile = new string[2]; Mobile [0] = "Samsuang"; Mobile [1] = "Sony"; Mobile [2] = "Nexus"; for(int i = 0; i < Mobile .Length; i++) { Console.WriteLine("Item number " + i + ": " + Mobile[i]); Console.ReadLine(); } } }
}
No comments
Post a Comment