-1

Isn't a string already a character array in c#? Why is there a explicit ToCharacterArray function? I stumbled upon this when I was looking upon ways to reverse a string and saw a few answers converting the string to a character array first before proceeding with the loop to reverse the string. I am a beginner in coding.

Sorry if this seems stupid, but I didn't get the answer by googling.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • Converting a string to a character array and reversing the array [does not reverse the string](https://stackoverflow.com/questions/228038/). – Dour High Arch Feb 10 '19 at 17:02

5 Answers5

2

Isn't a string already a character array in c# ?

The underlying implementation is, yes.

But you are not allowed to directly access that. String is using encapsulation to be an immutable object.

The actual array is private and hidden from view. You can use an indexer (property) to read characters but you cannot change them. The indexer is read only.

So yes, you do need ToCharacterArray() for reversing and similar actions. Note that you always end up with a different string, you cannot alter the original.

H H
  • 263,252
  • 30
  • 330
  • 514
1

Isn't a string already a character array in c# ?

No, a string is a CLASS that encapsulates a "sequential collection of characters" (see Docs). Notice it doesn't explicitly say an "Array of Char". Now, it may be true that the string class currently uses a character array to accomplish this, but that doesn't mean it ~must~ use a character array to achieve that end. This is a fundamental concept of Object Oriented Programming that combines information hiding and the idea of a "black box" that does something. It doesn't matter how the black box (class) accomplishes its task under the hood, as long is it doesn't change the public interface presented to the end user. Perhaps, in the next version of .Net, some new-fangled magical structure that is not an array of characters will be used to implement the string class. The end user may not be aware that this change has even occurred because they can still use the string class in the same way, and if they so desire, could still output the characters to an array with ToCharArray()...even though internally the string is no longer an array of characters.

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
0

Yes String type is a character array but string array is not an character array you must have to convert each string in your array in char type so that you can easily reverse its indexes and then convert it into temporary string and then add that string to array to be reversed

  • Thanks, but I wasn't talking about a "string array". I was talking about reversing a "single" string. If its already a character array, whats the need to convert it to a character array before proceeding to reverse the new character array. – Yuri Gagarin Feb 10 '19 at 16:09
  • Sorry but i couldn't understand earlier , Now i get it just going to add another answer – Muhammad Moid Shams Feb 10 '19 at 16:13
0

Internally, the text is stored as a sequential read-only collection of Char objects.

See Programming Guide Docs

abestrad
  • 898
  • 2
  • 12
  • 23
0
    Console.WriteLine(StringHelper.ReverseString("framework"));
    Console.WriteLine(StringHelper.ReverseString("samuel"));
    Console.WriteLine(StringHelper.ReverseString("example string"));

OR

public static string ReverseString(string s)
{
    char[] arr = s.ToCharArray();
    Array.Reverse(arr);
    return new string(arr);
}