
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中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号