Question

Isn't having String enough?

Just as an example,
why can String class lets you pass either char array or string array to its Split method?
while it's perfectly valid to use just string version?

  1. String.Split Method (Char[], StringSplitOptions)
  2. String.Split Method (String[], StringSplitOptions)
Was it helpful?

Solution

Strings are an abstraction for arrays of characters. As such, they are technically reference types (although, unlike most references types, they are immutable) even for single-char strings. Characters, on the other hand are actually value types themselves. (MSDN 1, MSDN 2)

C# Replace Performance shows a performance implication of this, in which using character arrays is 4x faster than using string arrays.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top