C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,997 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello
Instead of:
For Each Match As Match In MatchCollection
StringList.Add(Match.Value)
Next
Is there a shorter and faster way to add all element values in MatchCollection to the StringList which is list of string?
Thanks.
you can use linq for shorter source code, but it will not be faster. if there are larger number of matches adding code may make it faster.
var size = MatchCollection.Count;
if (size < 20) // use perf timing to optimize
{
foreach (var match in MatchCollection) StringList.Add(Match.Value);
}
else
{
var list = new List<string>(size); // preallocate
foreach (var match in MatchCollection) list.Add(Match.Value);
StringList.AddRange(list); // uses preallocation
}
note: compared to the performance of RegEx, this is probably not worth the effort. also be sure you are using precompiled regex expressions.