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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:55:11+00:00 2026-06-01T23:55:11+00:00

I am trying to iterate over all objects in a List with array subscription

  • 0

I am trying to iterate over all objects in a List with array subscription operator and change a field in them, and then store a reference of them in another list. Here is the code I have written:

private List<JavaScriptEventM> getSubset(List<JavaScriptEventM> domEvents)
    {
        List<JavaScriptEventM> retVal = new List<JavaScriptEventM>();

        for (int i = 0; i < domEvents.Count; i++)
        {
            JavaScriptEventM e = domEvents[i];
            e.xpath = getXPathToNode(e.source , false);
            retVal.Add(e);
        }

        return retVal;
    }

This code is supposed to access all members of a list called domEvents and change their xpath public field and store the reference also in another list (called retVal)

I call this function and pass a List of JavaScriptEventM objects that have their xpath field set to null. Strangely, after the function returns, the objects returned in retVal have their xpath field changed, but the original domEvents list passed as parameter to this function remains untouched. i.e the objects in the original list still have their xpath field set to null.

Is it that C# actually gets a clone of the object when you access it through array subscription operator on a List? How can I access all elements of a List one by one and change them?

  • 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-01T23:55:12+00:00Added an answer on June 1, 2026 at 11:55 pm

    Do not use struct in C# unless you know exactly what it is doing. I.e. struct’s value semantics leads to problem like your one where original data does not get changed.

    Unlike C++ there is a huge difference between struct and class in C#.

    Classes and Structs

    What's the difference between struct and class in .NET?.

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

Sidebar

Related Questions

I'm trying to iterate over all main menu's items. Problem starts at sub menus
I'm trying to write an algorithm that will let me iterate over all desired
I'm using JSF/Facelets, and I'm trying to iterate over some Document objects (custom object)
I've been trying to figure out how to iterate over the list of columns
Im trying to iterate over a nestled dict list. The first level works fine.
I'm trying to iterate over the members of a static class and invoke all
I need to iterate over all request header objects and print it in App
I'm trying to iterate over checkboxes in an ASP.Net web page, and if any
I'm trying to iterate over an enum, and call a method using each of
I have a const char arr[] parameter that I am trying to iterate over,

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.