C# | ToCharArray() Method Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In C#, ToCharArray() is a string method. This method is used to copy the characters from a specified string in the current instance to a Unicode character array or the characters of a specified substring in the current instance to a Unicode character array. This method can be overloaded by changing the number of arguments passed to it. Syntax: public char[] ToCharArray() or public char[] ToCharArray(int startIndex, int length) Explanation: public char[] ToCharArray() method will take no parameter. This method will return character array. Character array elements will contain the individual characters of the string. So basically this method will copy each character of a string to the character array. The first character of the string will be copied at index zero of the character array and the last character will be copied at index Array.Length – 1. When we create a string from the characters in a character array, then it will call String(Char[]) constructor. public char[] ToCharArray(int startIndex, int length) method will take two parameters startIndex which is used to specify the starting position of the substring and its type is System.Int32. Another parameter is length which is used to specify the length of the substring and its type is System.Int32. The startIndex parameter is zero-based, means the index of the first character in the string instance can be zero. Important Points: public char[] ToCharArray(int startIndex, int length) method can give the exception ArgumentOutOfRangeException if startIndex or length is less than zero or (startIndex + length) is greater than the length of current string instance. If the specified length is zero then the returned array will be empty and will have zero length. If current or this instance is null or an empty string ("") then the returned array will be empty will have length zero. Below are the programs to demonstrate the above Methods : Program 1: To illustrate the ToCharArray() Method : csharp // C# program to demonstrate // ToCharArray() method using System; class Geeks { // Main Method public static void Main() { String str = "GeeksForGeeks"; // copy the string str to chars // character array & it will start // copy from 'G' to 's', i.e. // beginning to ending of string char[] chars = str.ToCharArray(); Console.WriteLine("String: " + str); Console.Write("Character array :"); // to display the resulted character array for (int i = 0; i < chars.Length; i++) Console.Write(" " + chars[i]); } } Output: String: GeeksForGeeks Character array : G e e k s F o r G e e k s Program 2:To illustrate the ToCharArray(int startIndex, int length) Method : csharp // C# program to demonstrate // ToCharArray(int startIndex, int length) // method using System; class Geeks { // Main Method public static void Main() { String str = "GeeksForGeeks"; // copy the string str to chars // character array & it will // start copy from 'F' to 'r' i.e. // copy from startindex of string // is 5 upto (startindex+length-1) i.e. // 5 + (3-1) has to copy char[] chars1 = str.ToCharArray(5, 3); Console.WriteLine("String: " + str); Console.WriteLine("\nCharacter array 1:"); // to display the resulted character array for (int i = 0; i < chars1.Length; i++) Console.WriteLine("Index " + i + " : " + chars1[i]); // copy the string str to chars // character array & it will // start copy from 'F' to 'k' i.e. // copy from startindex of string // 5 upto (startindex+length-1) i.e. // 5 + (7-1) char[] chars2 = str.ToCharArray(5, 7); Console.Write("\nCharacter array 2:"); // to display the resulted character // array using foreach loop foreach(char c in chars2) Console.Write(" " + c); } } Output: String: GeeksForGeeks Character array 1: Index 0 : F Index 1 : o Index 2 : r Character array 2: F o r G e e k References : https://learn.microsoft.com/en-us/ https://learn.microsoft.com/en-us/ Comment More infoAdvertise with us M Mithun Kumar Follow Improve Article Tags : Misc C# CSharp-method CSharp-string Explore IntroductionC# Tutorial 4 min read Introduction to .NET Framework 6 min read C# .NET Framework (Basic Architecture and Component Stack) 6 min read C# Hello World 2 min read Common Language Runtime (CLR) in C# 4 min read FundamentalsC# Identifiers 2 min read Data Types in C# 6 min read C# Variables 4 min read C# Literals 5 min read Operators in C# 7 min read C# Keywords 5 min read Control StatementsC# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch) 5 min read C# Switch Statement 4 min read Loops in C# 4 min read C# Jump Statements (Break, Continue, Goto, Return and Throw) 4 min read OOP ConceptsC# Class and Objects 5 min read Constructors in C# 5 min read C# Inheritance 6 min read C# Encapsulation 4 min read C# Abstraction 4 min read MethodsMethods in C# 4 min read Method Overloading in C# 4 min read C# | Method Parameters 7 min read C# Method Overriding 9 min read Anonymous Method in C# 3 min read ArraysArrays in C# 6 min read Jagged Arrays in C# 4 min read Array Class in C# 5 min read How to Sort an Array in C# | Array.Sort() Method Set - 1 8 min read How to find the rank of an array in C# 2 min read ArrayListArrayList in C# 6 min read C# ArrayList Class 7 min read C# | Array vs ArrayList 2 min read StringStrings in C# 6 min read C# Verbatim String Literal - @ 5 min read C# String Class 9 min read C# StringBuilder 4 min read C# String vs StringBuilder 3 min read TupleC# Tuple 7 min read C# Tuple Class 3 min read C# ValueTuple 7 min read C# ValueTuple Struct 4 min read IndexersC# Indexers 4 min read C# Multidimensional Indexers 5 min read C# - Overloading of Indexers 3 min read Like