Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Declare char arrays in C#
A char array in C# is used to store a sequence of characters. You can declare and initialize char arrays in several ways, depending on whether you know the values at compile time or need to set them dynamically.
Syntax
Following are the different ways to declare a char array −
// Declaration with size
char[] arr = new char[size];
// Declaration with initialization
char[] arr = {'a', 'b', 'c'};
// Alternative initialization syntax
char[] arr = new char[] {'a', 'b', 'c'};
Declaring and Setting Elements Individually
You can declare a char array with a specific size and then assign values to individual elements −
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 char elements...");
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine(arr[i]);
}
}
}
The output of the above code is −
Displaying char elements... h a n k s
Declaring with Direct Initialization
You can declare and initialize a char array in a single statement −
using System;
public class Program {
public static void Main() {
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
char[] consonants = new char[] {'b', 'c', 'd', 'f', 'g'};
Console.WriteLine("Vowels:");
foreach (char c in vowels) {
Console.Write(c + " ");
}
Console.WriteLine("\nConsonants:");
foreach (char c in consonants) {
Console.Write(c + " ");
}
}
}
The output of the above code is −
Vowels: a e i o u Consonants: b c d f g
Converting String to Char Array
You can also create a char array from a string using the ToCharArray() method −
using System;
public class Program {
public static void Main() {
string word = "hello";
char[] charArray = word.ToCharArray();
Console.WriteLine("Original string: " + word);
Console.WriteLine("Char array elements:");
for (int i = 0; i < charArray.Length; i++) {
Console.WriteLine("Index " + i + ": " + charArray[i]);
}
// Converting back to string
string reconstructed = new string(charArray);
Console.WriteLine("Reconstructed string: " + reconstructed);
}
}
The output of the above code is −
Original string: hello Char array elements: Index 0: h Index 1: e Index 2: l Index 3: l Index 4: o Reconstructed string: hello
Common Operations
| Operation | Description | Example |
|---|---|---|
| Access element | Get or set element at specific index | arr[0] = 'A'; |
| Get length | Number of elements in array | arr.Length |
| Convert to string | Create string from char array | new string(arr) |
Conclusion
Char arrays in C# can be declared with a fixed size and populated individually, or initialized directly with values. They are useful for character manipulation and can be easily converted to and from strings using ToCharArray() and the string constructor.
