首頁  >  文章  >  後端開發  >  如何在C#中存取數組元素?

如何在C#中存取數組元素?

WBOY
WBOY轉載
2023-08-22 11:33:121301瀏覽

如何在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#中存取數組元素?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除