Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8283417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:43:31+00:00 2026-06-08T10:43:31+00:00

In answer to the following question: How to convert MatchCollection to string array Given

  • 0

In answer to the following question:
How to convert MatchCollection to string array

Given The two Linq expressions:

var arr = Regex.Matches(strText, @"\b[A-Za-z-']+\b")
    .OfType<Match>() //OfType
    .Select(m => m.Groups[0].Value)
    .ToArray();

and

var arr = Regex.Matches(strText, @"\b[A-Za-z-']+\b")
    .Cast<Match>() //Cast
    .Select(m => m.Groups[0].Value)
    .ToArray();

OfType<> was benchmarked by user Alex to be slightly faster (and confirmed by myself).

This seems counterintuitive to me, as I’d have thought OfType<> would have to do both an ‘is’ comparison, and a cast (T).

Any enlightenment would be appreciated as to why this is the case 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:43:33+00:00Added an answer on June 8, 2026 at 10:43 am

    My benchmarking does not agree with your benchmarking.

    I ran an identical benchmark to Alex’s and got the opposite result. I then tweaked the benchmark somewhat and again observed Cast being faster than OfType.

    There’s not much in it, but I believe that Cast does have the edge, as it should because its iterator is simpler. (No is check.)

    Edit: Actually after some further tweaking I managed to get Cast to be 50x faster than OfType.

    Below is the code of the benchmark that gives the biggest discrepancy I’ve found so far:

    Stopwatch sw1 = new Stopwatch();
    Stopwatch sw2 = new Stopwatch();
    
    var ma = Enumerable.Range(1, 100000).Select(i => i.ToString()).ToArray();
    
    var x = ma.OfType<string>().ToArray();
    var y = ma.Cast<string>().ToArray();
    
    for (int i = 0; i < 1000; i++)
    {
        if (i%2 == 0)
        {
            sw1.Start();
            var arr = ma.OfType<string>().ToArray();
            sw1.Stop();
            sw2.Start();
            var arr2 = ma.Cast<string>().ToArray();
            sw2.Stop();
        }
        else
        {
            sw2.Start();
            var arr2 = ma.Cast<string>().ToArray();
            sw2.Stop();
            sw1.Start();
            var arr = ma.OfType<string>().ToArray();
            sw1.Stop();
        }
    }
    Console.WriteLine("OfType: " + sw1.ElapsedMilliseconds.ToString());
    Console.WriteLine("Cast: " + sw2.ElapsedMilliseconds.ToString());
    Console.ReadLine();
    

    Tweaks I’ve made:

    • Perform the “generate a list of strings” work once, at the start, and “crystallize” it.
    • Perform one of each operation before starting timing – I’m not sure if this is necessary but I think it means the JITter generates code beforehand rather than while we’re timing?
    • Perform each operation multiple times, not just once.
    • Alternate the order in case this makes a difference.

    On my machine this results in ~350ms for Cast and ~18000ms for OfType.

    I think the biggest difference is that we’re no longer timing how long MatchCollection takes to find the next match. (Or, in my code, how long int.ToString() takes.) This drastically reduces the signal-to-noise ratio.

    Edit: As sixlettervariables pointed out, the reason for this massive difference is that Cast will short-circuit and not bother casting individual items if it can cast the whole IEnumerable. When I switched from using Regex.Matches to an array to avoid measuring the regex processing time, I also switched to using something castable to IEnumerable<string> and thus activated this short-circuiting. When I altered my benchmark to disable this short-circuiting, I get a slight advantage to Cast rather than a massive one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Similar posts such as the following do not answer my question. Convert a string
Updated question given Andrew Hare's correct answer: Given the following C# classes: public class
I have the following code adapted from a previous question/answer: var str = $('title').text();
I am trying to answer the following question as part of my college revision:
I'm trying to answer the following question out of personal interest: What is the
I can not really find a satisfying answer to the following question: What would
For the following question, I am looking for an answer that is based on
I was asked the following question and I didn't know how to answer it.
Following this question and answer , I still have a bit trouble in the
I found the following answer to that question: A service opens before you even

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.