MatchCollection Contains

ANB 181 Reputation points

I have the following regex that will return me 4 items:
MatchCollection matchesFiles = Regex.Matches(content, regexFiles, RegexOptions.IgnoreCase | RegexOptions.Singleline);

[0] { file="banner.jpg" width="100" }
[1] { file="footer.jpg" width="100" }
[2] { file="leftSide.jpg" width="50" }
[3] { file="footer.jpg" width="100" }

Now I want to add to a List, all of them except duplications:
List<FileModel> fileArray = new List<FileModel>();

foreach (Match m in matchesFiles )
if (!matchesFiles.Contains(m.file) {
fileArray.Add( ... )

But somehow contains doesnt work...
Why ?

A set of technologies in the .NET Framework for building web applications and XML web services.
3,184 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 23,306 Reputation points Microsoft Vendor

    Hi @ANB ,
    The MatchCollection.ICollection<Match>.Contains(Match) method determines whether the collection contains a specific value.

    bool ICollection<Match>.Contains (System.Text.RegularExpressions.Match item);

    You can check
    Maybe you can change the method, you can refer to this demo:

    public ActionResult Index()  
         List<FileModel> matchesFiles = new List<FileModel>()  
                     new FileModel { file="banner.jpg", width="100" },  
                     new FileModel { file="footer.jpg", width="100" },  
                     new FileModel { file="leftSide.jpg", width="50" },  
                     new FileModel { file="footer.jpg", width="100" },  
                List<FileModel> fileArray = new List<FileModel>();  
                foreach (FileModel item in matchesFiles)  
                    if (!fileArray.Any(f => f.file == item.file))  
                ViewBag.fileArray = fileArray;  
                return View();  


    public class FileModel  
            public string file { get;set;}  
            public string width { get; set; }  


        <meta name="viewport" content="width=device-width" />  
        <table border="1" cellpadding="2" cellspacing="2">  
            @foreach (var fileArray in ViewBag.fileArray)  

    Best regards,
    Lan Huang

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments