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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:11:24+00:00 2026-05-26T11:11:24+00:00

I am trying to use an OrderedDictionary as an argument for the below extension

  • 0

I am trying to use an OrderedDictionary as an argument for the below extension function (by the way if you think collection.Reverse().Where(i => predicate(i)).Take(n) would be better, let me know!), but the generic OrderedDictionary implementation I use (based on this version) does not implement IList.

The collection only needs to provide access to its elements with an integer-based index (which OrderedDictionary does).

Is there another way to specify this requirement, without creating an overload?

/// <summary>
/// Return n elements, starting from the end of collection, that satisfy predicate.
/// </summary>
/// <param name="collection"></param>
/// <param name="predicate"></param>
/// <param name="n">Max number of elements to return</param>
/// <returns></returns>
public static IEnumerable<T> LastBefore<T>(this IList<T> collection, Func<T, bool> predicate, int n) {
    if (collection == null) 
    {
        throw new ArgumentNullException("collection");
    }
    for (int i = collection.Count - 1; i >= 0; i--) 
    {
        if (n == 0) break;
        if (predicate(collection[i])) 
        {
            --n;
            yield return collection[i];
        }
    }
}
  • 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-26T11:11:25+00:00Added an answer on May 26, 2026 at 11:11 am

    I would suggest defining an IReadableList interface; were it not for the fact that a read-write indexer does not count as an implementation of a read-only indexer, such a thing could have been a parent of IList (IMHO, it should have been anyway). Existing IList implementations won’t implement IReadableList unless wrapped, alas, but having a read-only list interface would make the concept of covariance much more useful, since an IReadableList<Cat> could be used as an IReadableList<Animal>.

    Incidentally, I tend to think that while IList<T> doesn’t inherit from the non-generic IList, perhaps it should. Any IList<T> could implement IList in type-safe fashion if the non-generic implementation is read-only. Even if one never actually read any objects from the non-generic IList, implementing it would make allow a routine which wants an IEnumerable<Animal> but is given an IList<Cat> [which of course implements IEnumerable<Cat>, and thus implicitly IEnumerable<Animal>] to get a Count of the collection without having to enumerate it.

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

Sidebar

Related Questions

I was trying use the code shown below to plot in such a way
I have been trying use the edit_post_link() function to contain an image. All of
Trying to use autdoc extension of sphinx but its not working. i kept docs
I am trying use a javascript function while passing php variables in it. For
I am trying use a javascript function while passing php variables in it. For
I am trying use the onClick() function in an activity for an android app.
I revieving the following error when trying use mysql_real_escape() function Fatal error: Call to
I was trying use a set of filter functions to run the appropriate routine,
I'm trying use self-signed certificate (c#): X509Certificate2 cert = new X509Certificate2( Server.MapPath(~/App_Data/myhost.pfx), pass); on
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The

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.