Declare char arrays in C#

CsharpProgrammingServer Side Programming

Declare a char array and set the size −

char[] arr = new char[5];

Now set the elements −

arr[0] = 'h';
arr[1] = 'a';
arr[2] = 'n';
arr[3] = 'k';
arr[4] = 's';

Let us see the complete code now to declare, initialize and display char arrays in C# −

Example

 Live Demo

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]);
      }
   }
}

Output

Displaying string elements...
h
a
n
k
s
raja
Published on 04-Sep-2018 07:56:10
Advertisements