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

  • Home
  • SEARCH
  • 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 567451
In Process

The Archive Base Latest Questions

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

A dictionary cannot be changed if we are looping over it in .Net. But

  • 0

A dictionary cannot be changed if we are looping over it in .Net. But why is the value in a dictionary read only. Any one has any ideas ?. Why .Net team decided on not changing the value when looping over dictionary. I can understand if the key cannot be changed but why value ?.
Also if you are using LINQ and getting the keys back in form of Ienumerable, can the value be changed ?. does lazy loading have a role to play ?

  • 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-13T13:05:03+00:00Added an answer on May 13, 2026 at 1:05 pm

    Why did the BCL team decide on not allowing changes to the value when looping over dictionary. I understand that the key cannot be changed but why not allow changes to the value?

    I cannot speak definitively for the team which built the dictionary, but I can make some educated guesses.

    First, it is often said that good programs are strict about the correctness of their outputs, but forgiving in what they accept. I do not believe this to be a good design principle. This is a bad design principle because it allows buggy callers to take dependencies on undefined and unsupported behaviour. It thereby creates a backwards-compatibility burden. (We certainly see this in the web browser world, where every browser vendor is pressured to be compatible with every other browser’s acceptance of incorrect HTML.)

    Components define contracts — they say what information they accept as input, what operations they support, and what results they produce. When you accept inputs that are not in your contract, or support operations that are not in your contract, what you’re doing is essentially making a new contract, a contract which is not documented, not supported, and could be broken in a future version. You’re basically building a time bomb and when it goes off, either a customer (who probably didn’t even know that they were doing something unsupported) gets broken, or the component provider ends up having to support forever a contract that they didn’t actually sign up for.

    It is therefore a good design principle to strictly enforce your contract. The contract of IEnumerable on a collection is “it’s illegal to modify the collection while iterating”. An implementer of this contract can choose to say “well, I happen to know that certain modifications are safe, so I’ll allow those”, and hey, suddenly you’re not implementing the contract anymore. You’re implementing a different, undocumented contract that people will come to rely on.

    It is better to simply enforce the contract, even if that’s unnecessary. That way, in the future, you have the freedom to rely upon your documented contract without worrying that some caller has broken the contract and gotten away with it, and expects to continue to be able to do so forever.

    It would be easy to design a dictionary that allowed mutation of values during iteration. Doing so prevents the component providers from ever being able to turn that feature off, and they are not required to provide it, so it is better to give an error when someone tries than to allow a caller to violate the contract.

    Second guess: the dictionary type is unsealed, and therefore can be extended. Third parties might extend the dictionary in such a manner that their invariants would be violated if a value were changed during an enumeration. Suppose, for example, that someone extends a dictionary in such a manner that it can be enumerated sorted by value.

    When you write a method that takes a Dictionary and does something to it, you assume that the operation will work on all dictionaries, even third party extensions. The designers of a component that is intended for extension need to be even more careful than usual to ensure that the object enforces its contract, because unknown third parties might be relying upon the enforcement of that contract. Because there might be a dictionary that cannot support changing a value during iteration, the base class should not support it either; to do otherwise is to violate the substitutability of derived classes for base classes.

    Also if you are using LINQ and getting the keys back in form of IEnumerable, can the value be changed ?

    The rule is that a dictionary may not be changed while iterating over it. Whether you’re using LINQ to do the iteration or not is irrelevant; iteration is iteration.

    does lazy loading have a role to play?

    Sure. Remember, defining a LINQ query does not iterate over anything; the result of a query expression is a query object. It is only when you iterate over that object that the actual iteration happens over the collection. When you say:

    var bobs = from item in items where item.Name == "Bob" select item;
    

    no iteration happens here. It’s not until you say

    foreach(var bob in bobs) ...
    

    that iteration over items happens.

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

Sidebar

Ask A Question

Stats

  • Questions 289k
  • Answers 289k
  • 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 You should add: #include <string> to your "article.h" header file… May 13, 2026 at 5:36 pm
  • Editorial Team
    Editorial Team added an answer A quick note on Mercurial (and distributed revision control systems… May 13, 2026 at 5:36 pm
  • Editorial Team
    Editorial Team added an answer I ran your code without any problems. You should to… May 13, 2026 at 5:36 pm

Related Questions

I'm working in VS 2010 and working on upgrading our application to .NET 4.
I'm hosting a WPF UserControl in a WinForms container. Now, I want to be
Background: I am generating the UI for a settings page. The settings are stored
Some background info; LanguageResource is the base class LanguageTranslatorResource and LanguageEditorResource inherit from LanguageResource
I am trying to build a pie chart from a dictionary. Before I display

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.