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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:51:23+00:00 2026-05-26T06:51:23+00:00

Given void ProcessSchedules(IEnumerable<Schedule> schedules) { Contract.Requires<ArgumentException>(Contract.ForAll(schedules,x => x.Date <= DateTime.Today)) foreach( var schedule in

  • 0

Given

void ProcessSchedules(IEnumerable<Schedule> schedules)
{
    Contract.Requires<ArgumentException>(Contract.ForAll(schedules,x => x.Date <= DateTime.Today))
    foreach( var schedule in schedules )
    {
       // Do something with schedule
    }
}

In this scenario, schedules will be enumerated twice. (Resharper gives me the warning) An alternative I do to most scenarios is to force the collection into a list at the beginning of the method, but given the nature of code contracts, no other code should precede the precondition. What’s the best way to handle this?

  • 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-26T06:51:23+00:00Added an answer on May 26, 2026 at 6:51 am

    Here are the options that I can think of in descending order of my personal preference.

    • Use static checking rather than run-time checking. This way it will not have to enumerate the list at all to do the check. This option has the limitation that it is extrememly expensive. I don’t have access to the static checker myself, as much as I would love to. So alternatively, you could…

    • Change the definition of the method to work OK in the presense of future dates so that the check is unnecessary.

    Like this:

    void ProcessSchedules(IEnumerable<Schedule> schedules) 
    { 
        Contract.Requires<ArgumentNullException>(schedules != null);
        foreach(var schedule in schedules ) 
        {
            if (schedule.Date <= DateTime.Today)
            { 
               // Do something with schedule 
            }
        } 
    } 
    

    If that doesn’t make sense for your program, then you could…

    • Change the definition of the method to accept a list in the first place.

    Like this:

    void ProcessSchedules(List<Schedule> schedules) 
    { 
        Contract.Requires<ArgumentException>(Contract.ForAll(schedules,x => x.Date <= DateTime.Today)) 
        foreach(var schedule in schedules ) 
        { 
           // Do something with schedule 
        } 
    } 
    

    You have already said that forcing it into a list would be an option, so clearly this is a finite sequence of acceptable size. There is a high probability that the caller is using a List<Schedule> in the first place, or that there are other places where this implementation would be useful.

    If you really want to keep the IEnumerable in your signature you could…

    • Just ignore the warning.

    With your original code:

    void ProcessSchedules(IEnumerable<Schedule> schedules) 
    { 
        Contract.Requires<ArgumentException>(Contract.ForAll(schedules,x => x.Date <= DateTime.Today)) 
        foreach( var schedule in schedules ) 
        { 
           // Do something with schedule 
        } 
    } 
    

    I seriously don’t see a problem in enumerating the list twice, and I am not even sure why Resharper would issue a warning for this.

    If generating the schedules enumeration is an expensive operation, it doesn’t matter, because you shouldn’t normally include the contract validation in your release code anyway.

    If the schedules enumeration is reading from a database, or something like that, then the contract isn’t valid anyway, because the data could change between the validation and the processing. In that case it would be more correct for the method to accept a list or some other cached copy of the data that will not mutate.

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

Sidebar

Related Questions

Given an operation contract such as: [OperationContract] void Operation(string param1, string param2, int param3);
Given: private void UpdataFieldItems(List<FieldItemBase> existingFieldItems, List<FieldItemBase> selectedFieldItems) { List<FieldItemBase> newFieldItemsSelected; var fieldSelectionChanges = GetFieldSelectionChanges(out
Given this piece of code: (void)someFunction(void) { int array[] = {1,2,3,4,5,6,7,8,9,10}; } Where are
Given the UINavigationController delegate methods: -(void)navigationController:(UINavigationController*)navigationController (will/did)ShowViewController:(UIViewController*)viewController animated:(BOOL)animated How do you tell or compare
Given: class A { public void m(List l) { ... } } Let's say
Given a declaration like this: class A { public: void Foo() const; }; What
Given the following class: class TestClass { public void SetValue(int value) { Value =
Given the following interfaces: interface IFoo { void Foo(); } interface IBar : IFoo
Given this piece of code : public static void writeFile(File file,List buffer)throws IOException{ File
Given a ParameterInfo p from this: void foo(int modopt(IsLong) n); p.GetOptionalCustomModifiers() returns a System.Runtime.CompilerServices.IsLong;

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.