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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:24:03+00:00 2026-05-25T13:24:03+00:00

I observe a very strange behavior, maybe could you help me to see what

  • 0

I observe a very strange behavior, maybe could you help me to see what happen.

Here the class:

public sealed class Sudoku
{
    private SudokuCell[] _grid = new SudokuCell[81];

    // ctor {}

    private IEnumerable<SudokuCell> Grid
    {
        get { return _grid; }
    }

    private SudokuRow[] _rows;
    public IEnumerable<SudokuRow> Rows
    {
        get
        {
            if (_rows == null)
            {
                _rows = new SudokuRow[9];

                for (int i = 0, length = 9; i < length; i++)
                {
                    _rows[i] = new SudokuRow(from cell in Grid
                                             where cell.Row == i
                                             select cell);

                    // Always print 9 (GOOD)
                    Trace.WriteLine("First Loop " + i + " : " + _rows[i].Cells.Count());
                }
            }

            for (int i = 0; i < 9; i++)
            {
                // Always print 0 ! Huh !?
                Trace.WriteLine("Second Loop " + i + " : " + _rows[i].Cells.Count());
            }

            return _rows;
        }
    }
}

public abstract class SudokuPart
{
    public SudokuPart(IEnumerable<SudokuCell> cells)
    {
        Cells = cells;
    }

    public int Index
    { get; protected set; }

    public IEnumerable<SudokuCell> Cells
    { get; protected set; }
}

public sealed class SudokuRow : SudokuPart
{
    public SudokuRow(IEnumerable<SudokuCell> cells)
        : base(cells)
    {
        base.Index = cells.First().Row;
    }
}

Could anyone tell me why in the second loop it trace 0 instead of 9 !? I changed nothing between both loops !!!

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-05-25T13:24:04+00:00Added an answer on May 25, 2026 at 1:24 pm

    This is the problem:

    _rows[i] = new SudokuRow(from cell in Grid
                             where cell.Row == i
                             select cell);
    

    That’s capturing the loop variable (i)… within the loop, it has a sensible value, which is why you’re seeing 9 matches.

    However, when you count the matching values in the second loop, that single captured variable will have the value 9. Now no cell.Row has a value of 9, so you’re not getting any matches. For more information on this, see Eric Lippert’s great blog post, “Closing over the loop variable considered harmful.”

    Three fixes:

    • Capture a copy of the loop variable:

      int copy = i;
      _rows[i] = new SudokuRow(from cell in Grid
                               where cell.Row == copy
                               select cell);
      

      Each iteration of the loop will get a separate copy.

    • Materialize the query in the loop:

      _rows[i] = new SudokuRow((from cell in Grid
                               where cell.Row == i
                               select cell).ToList());
      

      Or even:

      _rows[i] = new SudokuRow(Grid.Where(cell => cell.Row == i).ToList());
      
    • Don’t use LINQ at all! Why not just have an array of arrays to represent the grid? That’s a much more natural approach, IMO.

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

Sidebar

Related Questions

We're suffering from a very strange issue with ViewPager here. We embed lists on
I am having Xcode 4.5. I have observed a very strange behavior.In previous versions
I have a very strange behavior in going on KnockoutJS 1.2.1 (Can't switch to
I observe the following strange behaviour in Vim 7.3: When I'm below a fold,
It was very confusing to me to observe this situation: Integer i = null;
Scenario is very rare, but quite simple: you define a generic class, then create
I have a VERY complex service host which consists of multiple DUPLEX services here.
While writing a Javascript inheritance function some time ago I noticed some very strange
I am stuck with a very strange issue. I hope that many of you
This is a very wide-ranging/vague question, but here goes. Apologies in advance. The app

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.