声明一个char数组并设置大小-
char[] arr = new char[5];
现在设置元素-
arr[0] = 'h'; arr[1] = 'a'; arr[2] = 'n'; arr[3] = 'k'; arr[4] = 's';
现在让我们看看完整的代码,以在C#中声明,初始化和显示char数组-
using System; public class Program { public static void Main() { char[] arr = new char[5]; arr[0] = 'h'; arr[1] = 'a'; arr[2] = 'n'; arr[3] = 'k'; arr[4] = 's'; Console.WriteLine("Displaying string elements..."); for (int i = 0; i < arr.Length; i++) { Console.WriteLine(arr[i]); } } }
输出结果
Displaying string elements... h a n k s