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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:04:07+00:00 2026-06-13T13:04:07+00:00

So, this one is pretty straight forward I think. Here is my code: Dictionary<int,

  • 0

So, this one is pretty straight forward I think.

Here is my code:

Dictionary<int, IEnumerable<SelectListItem>> fileTypeListDict = new Dictionary<int, IEnumerable<SelectListItem>>();

foreach (PresentationFile pf in speakerAssignment.FKPresentation.PresentationFiles)
{
    IEnumerable<SelectListItem> fileTypes = Enum.GetValues(typeof(PresentationFileType))
                .Cast<PresentationFileType>().Select(x => new SelectListItem
        {
             Text = x.ToString(),
             Value = Convert.ToString((int)x),
             Selected = pf.Type == (int)x
        });

        fileTypeListDict.Add(pf.ID, fileTypes);
}

What is happening is that in the end the dictionary will have all the correct keys, but all of the values will be set to the fileTypes list created during the final iteration of the loop. I am sure this has something to do with the object being used as a reference, but have not seen this issue before in my time with C#. Anyone care to explain why this is happening and how I should resolve this issue?

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-13T13:04:08+00:00Added an answer on June 13, 2026 at 1:04 pm

    This is the infamous “foreach capture issue”, and is “fixed” in C# 5 (“fixed” is a strong word, since it suggests it was a “bug” before: in reality – the specification is now changed to acknowledge this as a common cause of confusion). In both cases, the lambda captures the variable pf, not “the value of pf during this iteration” – but in C# before-5 the variable pf is technically scoped outside the loop (so there is only one of them, period), where-as in C# 5 and above the variable is scoped inside the loop (so there is a different variable, for capture purposes, per iteration).

    In C# 4, just cheat:

    foreach (PresentationFile tmp in speakerAssignment.FKPresentation.PresentationFiles)
    {
        PresentationFile pf = tmp;
        //... as before
    

    now pf is scoped inside the foreach, and it will work OK. Without that, there is only one pf – and since you’ve deferred execution until the end, the value of the single pf will be the last iteration.

    An alternative fix would be: don’t defer execution:

    fileTypeListDict.Add(pf.ID, fileTypes.ToList()); // note the ToList
    

    Now it is evaluated while the pf is “current”, so will have the results you expected.

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

Sidebar

Related Questions

I'm pretty confused by this one and can't think of anything obvious that I
Im still pretty new so bear with me on this one, my question(s) are
Still pretty new to ObjectiveC and Cocoa here. Hopefully the answer to this question
Thought this would be pretty straight forward, but my value is remaining the same
This should be an easy one. Seems to be so straight forward that I
This one has me pretty rattled so I thank you in advance for your
I've been pretty puzzled by this one. It seems as though my implementation of
Normally I'm pretty competent with CSS but I just can't figure this one out...
Okay, I need help. I'm usually pretty good at SQL queries but this one
I'm reading this introductory book on parsing (which is pretty good btw) and one

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.