The linked question compares string
vs. char array
and all the replies give preference to char array
as far as performance is concerned. On the other hand, some useful functions, such as split
, are provided for string
, but not for char array
, which encourages the use of string
. What are the benefits of string
besides immutability (which is not needed in many applications)?
Asked
Active
Viewed 62 times
0

AlwaysLearning
- 7,257
- 4
- 33
- 68
-
It's the far more commonly used idiom, mainly **because** of it's immutability. Therefore most APIs only accept `string` – Charlieface Jan 12 '21 at 09:49
-
1If you are so concerned about performance that you are contemplating differences between string and char array then maybe .NET is not appropriate tool for your needs. – Dialecticus Jan 12 '21 at 10:09
-
@Charlieface Immutability is important for preventing race conditions that do not appear in most applications. So why would it be by far the most used idiom? – AlwaysLearning Jan 12 '21 at 10:32
-
Arguably, ease of use. – 500 - Internal Server Error Jan 12 '21 at 12:27
-
I think that it's a good an object oriented abstraction which hides implementation details effectively. What would you do if your codebase were written using `char[]`, because it's more effective, but later .NET team changes `string` implementation so that it's much faster than `char[]`. If you used the abstraction in the first place, it would much easier to upgrade to benefit with those changes (because you already use `string` and they only changed implementation). In the case of `char[]` upgrade seems to be much harder, `cause you need to update your API. – E. Shcherbo Jan 12 '21 at 13:12
-
@500-InternalServerError My question is precisely why things that make strings easy to use aren't provided for char array. – AlwaysLearning Jan 12 '21 at 13:18