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 936353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:18:08+00:00 2026-05-15T21:18:08+00:00

I have a collection of objects that I would like to iterate through to

  • 0

I have a collection of objects that I would like to iterate through to ultimately recognize some type of pattern. Currently I have a bunch of if statements that checks a flag for the expression. For example I have something along the lines of if (!foundFirstObjectOfTypeA). Once I found the first object of type A I would set the flag to true and we would no longer execute the block and move on to the next block. I have several types of patterns to recognize and because of this it has created a large block of if statements that are hard to understand and ugly.

Is there a way to peek/look past the identifier without the evaluation of the expression in the foreach loop? If I could do something along the lines of if (identifer == ObjectA && identifer.next == ObjectB) it would make my code much more readable and understandable and I could deal without having to set flags.

If there is no direct method/way does someone have some clever workarounds to simulate the effects I desire?

  • 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-05-15T21:18:08+00:00Added an answer on May 15, 2026 at 9:18 pm

    Use a for loop instead to increment i, then just look at object[i] and object[i+1]. What type of collection are you using? A list? An array? All a foreach loop does is hide the counter from you. There’s absolutely no reason to use a foreach loop instead of a for loop, if I understand your situation.

    This should work for any list and any sequence of objects:

    Function:

    public static bool ContainsSequence
        (System.Collections.Generic.IList<object> list, params object[] seq)
    {
        for (int i = 0; i < list.Count() - seq.Count() + 1; i++)
        {
            int j;
            for (j = 0; j < seq.Count(); j++)
            {
                if (list[i + j] != seq[j])
                    break;
            }
            if (j == seq.Count())
                return true;
        }
        return false;
    }
    

    Usage:

    private static void Main(string[] args)
    {
        var A = new object();
        var B = new object();
        var C = new object();
        var D = new object();
    
        var list = new[] {A, B, C, D};
        Console.WriteLine(ContainsSequence(list, B, C, D));
        Console.WriteLine(ContainsSequence(list, A, D, C, B, A, C));
        Console.WriteLine(ContainsSequence(list, A, B));
    }
    

    Output:

    True
    False
    True
    

    The function can be made simpler if you know how many objects you’re looking for (if you’re only looking for two, it’s almost Hamid Nazari‘s answer – you just need to add AndrewS‘s bound checking, below.)

    If you want the index of the start of the sequence, you can change the return type of my function to an int and return i instead of true (and return -1 instead of false.)

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

Sidebar

Related Questions

I have a collection of objects that I would like to partition into two
I have an observable collection of objects that I'd like to display on the
I have a collection FileTypes that holds objects of type FileType. That type has
I have a collection of objects stored in a List. I would like to
I have a javascript array of objects that I would like to use to
I'm looking to have a collection of objects that implement a certain interface, but
I have a list/collection of objects that may or may not have the same
I have a Queue that contains a collection of objects, one of these objects
I have an ItemsControl that is bound to a collection of objects. Each object
I have several methods that populate a SQLCommand objects parameter collection from an object

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.