Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is string actually an array of chars or does it just have an indexer?

Tags:

arrays

string

c#

As the following code is possible in C#, I am intersted whether string is actually an array of chars:

string a="TEST";
char C=a[0]; // will be T
like image 299
Kalamro Avatar asked Sep 08 '10 15:09

Kalamro


People also ask

Are strings just arrays of chars?

1) Strictly speaking, yes, a String in . NET is an array of characters.

Is a string an array of chars in JavaScript?

The string in JavaScript can be converted into a character array by using the split() and Array. from() functions. Using String split() Function: The str. split() function is used to split the given string into array of strings by separating it into substrings using a specified separator provided in the argument.

Are strings stored as arrays?

When strings are declared as character arrays, they are stored like other types of arrays in C. For example, if str[] is an auto variable then string is stored in stack segment, if it's a global or static variable then stored in data segment, etc.

Is string an array in C#?

An array is a collection of the same type variable. Whereas a string is a sequence of Unicode characters or array of characters. Therefore arrays of strings is an array of arrays of characters.


1 Answers

System.String is not a .NET array of Char because this:

char[] testArray = "test".ToCharArray();

testArray[0] = 'T';

will compile, but this:

string testString = "test";

testString[0] = 'T';

will not. Char arrays are mutable, Strings are not. Also, string is Array returns false, while char[] is Array returns true.

like image 197
FacticiusVir Avatar answered Nov 15 '22 03:11

FacticiusVir