如何在C#中访问数组元素?

如何在C#中访问数组元素?

First, define and initalize an array −

int[] p = new int[3] {99, 92, 95};登录后复制

for (j = 0; j < 3; j++ ) { Console.WriteLine("Price of Product[{0}] = {1}", j, p[j]); }登录后复制

p[2];登录后复制

现在让我们看完整的代码 −

示例

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(); } } }登录后复制