Home >Backend Development >C#.Net Tutorial >How to access array elements in C#?
First, define and initalize an array −
int[] p = new int[3] {99, 92, 95};
Now, display the array elements −
for (j = 0; j < 3; j++ ) { Console.WriteLine("Price of Product[{0}] = {1}", j, p[j]); }
To access any element, just include like this Index of desired element −
p[2];
The above is how to access the third element.
Now let’s see the complete code −
using System; namespace Program { class Demo { static void Main(string[] args) { int[] p = new int[3] {99, 92, 95}; int j; for (j = 0; j < 3; j++ ) { Console.WriteLine("Price of Product[{0}] = {1}", j, p[j]); } // access int e = p[2]; Console.WriteLine("Product 3rd price: "+e); Console.ReadKey(); } } }
The above is the detailed content of How to access array elements in C#?. For more information, please follow other related articles on the PHP Chinese website!