question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IDE0220 on foreach (Match m in regex.Matches) – Should Not Occur

See original GitHub issue

Using the following code snippet results in Roslyn analyzer adding an informational warning to the list of analysis results:

foreach (Match m in regex.Matches(text))
{
  ...
}
IDE0220  'foreach' statement implicitly converts 'object' into 'System.Text.RegularExpressions.Match'. Add an explicit cast to indicate your intention as it may fail at runtime.

MatchCollection.Item[], however, doesn’t return object. It returns a Match.

Version Used

.NET 6

Steps to Reproduce

  1. Create a regular expression object.
  2. Run the Regex.Matches() method on that regular expression and feed immediately to a Match variable in a foreach loop.

Expected Behavior

This should be analyzed by the Roslyn analyzer without flaw.

Actual Behavior

The Roslyn analyzer adds an information to the analysis result (roughly translated by me from German to English):

IDE0220  'foreach' statement implicitly converts 'object' into 'System.Text.RegularExpressions.Match'. Add an explicit cast to indicate your intention as it may fail at runtime.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:4
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
m-gallesiocommented, Mar 28, 2023

This still applies to .NET 7.0.

1reaction
twest820commented, Aug 19, 2022

That looks like #60120 rather than #60210. MatchCollection defines

    public IEnumerator GetEnumerator() => new Enumerator(this);

    IEnumerator<Match> IEnumerable<Match>.GetEnumerator() => new Enumerator(this);

with Enumerator having

        public Match Current { get { ... } }

        object IEnumerator.Current => Current;

Naively, I’d expect both foreach and the rule to find the strongly typed path but perhaps that’s not what happens.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Regex.Matches Fails To Find Multiple Matches [duplicate]
The problem is, it returns one match and not 4. Why are the in-between patterns ignored? Do you know what I am missing?...
Read more >
Regex.Matches Method (System.Text.RegularExpressions)
Searches the specified input string for all occurrences of a specified regular expression, using the specified matching options and time-out interval.
Read more >
c# - Regex to first match, then replace found matches
For each word, to find out if a string I am given has any matches. If it does, I perform some (slightly costly)...
Read more >
Assign each RegEx Match to Data Row - Help
The issue I am having is only the first match from each pdf document is being assigned to a row within the data...
Read more >
When I use foreach for regex.matches, var thinks its type is ...
When you cast to IEnumerable<Match> first, then it bind to the explicit interface method. foreach(var m in (IEnumerable<Match>)matches) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found