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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:40:54+00:00 2026-05-31T07:40:54+00:00

var names = new[] { new { Name = John, Age = 44 },

  • 0
                var names = new[] { 
                            new { Name = "John", Age = 44 },
                            new { Name = "Diana", Age = 45 },
                            new { Name = "James", Age = 17 },
                            new { Name = "Francesca", Age = 15} 
                            };

            for (int i = 0; i < names.Length; i++)
            {
                names[i].Age = 23; //-------->Error
                names[i] = new { Name = "XYX", Age = 26 }; //----->Works fine
            }

            foreach(var name in names)
            {
                name.Age = 1;  //-------->Error
                name = new { Name = "ABC", Age = 25 };  //-------->Error
            }

I have two questions here.
1. Why I was not able to change the any attribute of an iteration variable.
2. I was only able to assign a new object to the iteration variable in for loop. Not in foreach loop. Why?

  • 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-31T07:40:55+00:00Added an answer on May 31, 2026 at 7:40 am

    Question 1: Why I was not able to change the any attribute of an iteration variable?

    From the documentation on Anonymous Types:

    Anonymous types provide a convenient way to encapsulate a set of read-only properties

    You cannot change the values of the properties in your anonymous type, so

    name.Age = 1;
    // and
    names[i].Age = 1; 
    

    are equally invalid.


    Question 2. I was only able to assign a new object to the iteration variable in for loop. Not in foreach loop. Why?

    From the documentation on IEnumerable:

    An enumerator remains valid as long as the collection remains unchanged.

    You would invalidate the iterator if you change the backing list in any way. Consider what would happen if the iterator returned the items in a specific order based on the Age field, for example.

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

Sidebar

Related Questions

JavaScript has object literals, e.g. var p = { name: John Smith, age: 23
var cityList = from country in doc.Element(result) .Element(cities) .Descendants(city) select new { Name =
i have this JSON: var projects_array = new Array( {name:myName1, id:myid1, index:1}, {name:myName2, id:myid2,
Consider the following criteria query: var x = SomeCriteria.AddOrder(new Order(Name, true)).List(); This will order
I have some Mongoose Models with geospacial indexes: var User = new Schema({ name
I have the following variable that accepts a file name: var xtr = new
Given var myHash = new Array(); myHash['key1'] = { Name: 'Object 1' }; myHash['key2']
select: function (event, ui) { var staffItem = new Object(); staffItem.StaffId = ui.item.id; staffItem.Name
say I have a list of names var names = new List<string>{Tom, Dick, Harry};
How can i concatenate var names to declare new vars in javascript?: var foo

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.