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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:30:15+00:00 2026-05-14T23:30:15+00:00

I’m trying to work out what’s going on with this code. I have two

  • 0

I’m trying to work out what’s going on with this code. I have two threads iterating over the range and I’m trying to understand what is happening when the second thread calls GetEnumerator(). This line in particular (T current = start;), seems to spawn a new ‘instance’ in this method by the second thread.

Seeing that there is only one instance of the DateRange class, I’m trying to understand why the second thread doesn’t capture the ‘current’ variable being modified by the first thread.

class Program {

        static void Main(string[] args) {

            var daterange = new DateRange(DateTime.Now, DateTime.Now.AddDays(10), new TimeSpan(24, 0, 0));

            var ts1 = new ThreadStart(delegate {

                foreach (var date in daterange) {
                    Console.WriteLine("Thread " + Thread.CurrentThread.ManagedThreadId + " " + date);
                }
            });

            var ts2 = new ThreadStart(delegate {

                foreach (var date in daterange) {
                    Console.WriteLine("Thread " + Thread.CurrentThread.ManagedThreadId + " " + date);
                }
            });

            Thread t1 = new Thread(ts1);

            Thread t2 = new Thread(ts2);

            t1.Start();
            Thread.Sleep(4000);
            t2.Start();

            Console.Read();
        }
    }

    public class DateRange : Range<DateTime> {

        public DateTime Start { get; private set; }
        public DateTime End { get; private set; }
        public TimeSpan SkipValue { get; private set; }


        public DateRange(DateTime start, DateTime end, TimeSpan skip) : base(start, end) {
            SkipValue = skip;
        }

        public override DateTime GetNextElement(DateTime current) {

            return current.Add(SkipValue);
        }
    }

    public abstract class Range<T> : IEnumerable<T> where T : IComparable<T> {

        readonly T start;
        readonly T end;


        public Range(T start, T end) {

            if (start.CompareTo(end) > 0)
                throw new ArgumentException("Start value greater than end value");

            this.start = start;
            this.end = end;
        }

        public abstract T GetNextElement(T currentElement);

        public IEnumerator<T> GetEnumerator() {

            T current = start;

            do {
                Thread.Sleep(1000);

                yield return current;

                current = GetNextElement(current);

            } while (current.CompareTo(end) < 1);
        }       

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
            return GetEnumerator();
        }
    }
  • 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-14T23:30:15+00:00Added an answer on May 14, 2026 at 11:30 pm

    They both use the same IEnumerable<T>, but different IEnumerator<T>s. Each time you enter a for each in loop with a IEnumerable, GetEnumerator is called, returning a separate IEnumerator with its own state.

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

Sidebar

Ask A Question

Stats

  • Questions 407k
  • Answers 407k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Leverage the concept of pluggable look and feels and select… May 15, 2026 at 6:26 am
  • Editorial Team
    Editorial Team added an answer I have a similar situation in mobile software we use,… May 15, 2026 at 6:26 am
  • Editorial Team
    Editorial Team added an answer This seems wrong. Your GtkWindow should be a pointer too,… May 15, 2026 at 6:26 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.