In answer to the following question: How to convert MatchCollection to string array
Given The two Linq expressions:
var arr = Regex.Matches(strText,
Just reverse the order of OfType
and Cast
in your method and you'll note that there is no difference. The first one always runs faster than the second one. This is a case of a bad microbenchmark.
Wrapping your code in a loop to run them in random order:
OfType: 1224
Cast: 2815
Cast: 2961
OfType: 3010
OfType: 3027
Cast: 2987
...
And then again:
Cast: 1207
OfType: 2781
Cast: 2930
OfType: 2964
OfType: 2964
OfType: 2987
...
Lifting out the Regex.Matches
, which appears to cause the problem:
Cast: 1247
OfType: 210
OfType: 170
Cast: 171
...
and
OfType: 1225
Cast: 202
OfType: 171
Cast: 192
Cast: 415
So, no. OfType
is not faster than Cast
. And no, Cast
is not faster than OfType
.