Declare char arrays in C#


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

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 22-Jun-2020

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements