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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:30:57+00:00 2026-06-14T18:30:57+00:00

The following gives answer as 1 in VS 2010 and 2 in VS 2012.

  • 0

The following gives answer as 1 in VS 2010 and 2 in VS 2012. I personally think it should be 2. I am not sure what’s going on here.

using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;

namespace _335ExamPreparation
{
    public class Doubts
    {
        int[] nums = { 10, 11, 12, 13, 14, 15, 16 };
        int[] divisors = { 7, 10 };

        static void Main(string[] args)
        {
            Doubts d = new Doubts();
            d.func();
        }

        public void func()
        {
            var m = Enumerable.Empty<int>();
            foreach (int d in divisors)
            {
                m = m.Concat(nums.Where(s => (s % d == 0)));
            }

            int count = m.Distinct().Count();
            Console.WriteLine(count);
        }
    }
}

Thanks.

  • 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-06-14T18:30:58+00:00Added an answer on June 14, 2026 at 6:30 pm

    What you’re seeing is the result of two different applications of foreach. The behavior was changed in VS 2012. See this article.

    The difference between the two involves the scope and lifetime of the d variable in the foreach loop. Before VS 2012, there was only one d variable, so what this means is that you are creating two copies of a closure (s => (s % d == 0))) that both reference the same d. After the loop finishes being evaluated, d is 10. When you execute the query by calling .Distinct().Count(), both closures will see a value of 10 for d. This is why the count is 1 on VS 2010.

    VS 2012 generates a different variable for each iteration1, so each closure will see a different instance of the d variable, the one that corresponds to that particular iteration.

    This is roughly the code that VS 2010 generates:

    int d;
    for (int _index = 0; _index < divisors.Length; ++_index) {
        d = divisors[_index];
        m = m.Concat(nums.Where(s => (s % d == 0)));
    }
    

    And this is roughly what VS 2012 generates:

    for (int _index = 0; _index < divisors.Length; ++_index) {
        int d = divisors[_index];
        m = m.Concat(nums.Where(s => (s % d == 0)));
    }
    

    The difference between these two should be readily apparent.

    If you want to get the same behavior no matter which VS version, then always copy your iteration variable:

    foreach (int d in divisors)
    {
        var copy = d;
        m = m.Concat(nums.Where(s => (s % copy == 0)));
    }
    

    1 Technically, only if the iteration variable is referenced in a closure. If it’s not then there is no need to make a copy as this only affects closure semantics anyway.

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

Sidebar

Related Questions

The following answer gives a solution using C#, I was wondering what the equivalent
Any ideas why does the entity framework in LINQ gives following strange error: Unable
I followed the top answer here, see below for code to get system memory
Following Quassnoi's answer I have edited to show what happens using his advice and
Possible Duplicate: Matlab gives wrong answer Can anyone explain to me why the following
Updated question given Andrew Hare's correct answer: Given the following C# classes: public class
The following gives me the error Uncaught SyntaxError: Unexpected identifier : $('span.xtro').html(''); $('span.xtro').html('<input type='button'
The following code gives digits whose name is shorter than their value. I can't
The following code gives seg fault and some time stalls/hangs and never works. for(i=0;i<360;i++)
The following command gives many manuals of Zsh man zsh<tab> alt text http://dl.getdropbox.com/u/175564/zsh.png I

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.