卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16333本站已运行3317

如何在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();
      }
   }
}

卓越飞翔博客
上一篇: C++中的虚拟函数和纯虚函数的应用技巧
下一篇: Python中如何使用__new__()函数创建对象实例
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏