-1

enter image description here

As you can see in the above picture I can use Regex class like a data source when declaring it. Why is that?

And I also noticed this LinQ lines are starting with dots. How is this possible?

Mr.Web
  • 6,992
  • 8
  • 51
  • 86

1 Answers1

0

Regex.Matches(string, string) returns a MatchCollection instance which implements ICollection and IEnumerable. So you can't use LINQ directly since the LINQ extension methods in System.Linq.Enumerable require IEnumerable<T>(the generic version, differences).

That's why Enumerable.OfType was used. This returns IEnumerable<Match>, so now you can use LINQ. Instead of OfType<Match> he could also have used Cast<Match>.

In general you can use Linq-To-Objects with any kind of type that implements IEnumerable<T>, even with a String since it implements IEnumerable<char>. A small example which creates a dictionary of chars and their occurrences:

Dictionary<char, int> charCounts = "Sample Text" // bad example because only unique letters but i hope you got it
  .GroupBy(c => c)
  .ToDictionary(g => g.Key, g => g.Count());

To answer the .dot part of your question. LINQ basically consists of many extension methods, so you call them also like any other method, you could use one line:

Dictionary<char, int> charCounts = "Sample Text".GroupBy(c => c).ToDictionary(g => g.Key, g => g.Count());
Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939