Phương thức SetValue() gán giá trị cho phần tử tại vị trí chỉ định trong mảng một chiều. Chỉ số là số nguyên 32-bit.
Ví dụ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
// Tạo mảng string kích thước 6
Array arr = Array.CreateInstance(typeof(String), 6);
// Gán giá trị
arr.SetValue("One", 0);
arr.SetValue("Two", 1);
arr.SetValue("Three", 3);
arr.SetValue("Four", 4);
arr.SetValue("Five", 5);
// Index 2 bị bỏ qua -> null
Console.WriteLine("Độ dài mảng: {0}", arr.GetLength(0));
// In toàn bộ
for (int i = 0; i < arr.Length; i++)
Console.WriteLine("arr[{0}] = {1}", i, arr.GetValue(i) ?? "null");
Console.ReadLine();
}
}
}
Kết quả
Độ dài mảng: 6 arr[0] = One arr[1] = Two arr[2] = null arr[3] = Three arr[4] = Four arr[5] = Five
Overloads của SetValue