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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:31:35+00:00 2026-05-25T16:31:35+00:00

In this scenario ( http://ideone.com/kxevv ): using System; using System.Linq; using System.Collections.Generic; public class

  • 0

In this scenario ( http://ideone.com/kxevv ):

using System;
using System.Linq;
using System.Collections.Generic;

public class Test{
    public static void Main(){
        List<Moo> list = new List<Moo>();
        list.Add(new Moo {A = "5", B = "10"});
        list.Add(new Moo {A = "6", B = "12"});

        var changeRef = list.Where(p=>p.A == "5").First();
        changeRef.B = "20";

        var changeVal = list.Where(p=>p.A == "6").First();
        changeVal = new Moo {A = "6", B = "24"};

        Console.WriteLine(string.Join(", ", list.Select(p =>p.B).ToArray())); //prints 20, 12
    }
}

public class Moo{
    public string A {get;set;}
    public string B {get;set;}
}

Is there some way to change ChangeVal by reference? Or better, why the print result isn’t 20, 24?

Or when I put the new modifier the context change? What’s the principle involved with this bahavior?

  • 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-25T16:31:36+00:00Added an answer on May 25, 2026 at 4:31 pm

    It looks to me like you want to find the index of the item where p => p.A == "6", and then set list[index] = new Moo{...};

    var index = list.FindIndex(p=>p.A == "6");
    list[index] = new Moo {A = "6", B = "24"};
    
    Console.WriteLine(string.Join(", ", list.Select(p =>p.B).ToArray())); //prints 20, 24
    

    If you were just working with an IEnumerable, however, this method would not be available. This is by design: the IEnumerable interface doesn’t give you any methods to change the data: you’re only supposed to iterate across it. You could, however, create a projection that would produce the same result without actually modifying the list itself:

    var query = list.Select(p => p.A == "6" ? new Moo {A = "6", B = "24"} : p);
    Console.WriteLine(string.Join(", ", list.Select(p =>p.B).ToArray())); //prints 20, 12
    Console.WriteLine(string.Join(", ", query.Select(p =>p.B).ToArray())); //prints 20, 24
    

    Concerning the principles that govern this, you definitely need to learn about the memory model used by .NET and Java. Unless you are using unsafe code, objects are considered to be value-by-reference. This means that when you change the value of changeVal via the = operator, you are literally pointing the variable itself to a different location, rather than replacing or modifying the memory at the location it currently points to. Spending time really grokking the memory model will help you more in job interviews and in real life than just about anything else you can learn about .NET and Java.

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

Sidebar

Related Questions

I have following scenario: alt text http://static.zooomr.com/images/7579022_e64808b855_o.png We have a WebService which poses as
Take this scenario for an example: User types http://example.com/index.html into my form Form is
So I ran into this exact problem: http://www.vistax64.com/powershell/273120-bug-when-using-namespace-parameter-new-webserviceproxy.html The gist of the issue is
Is this scenario even possible? class Base { int someBaseMemer; }; template<class T> class
How would you test this scenario? I've just started looking into NHibernate and having
I was reading about this scenario where making use of the C# using statement
I am using this code... Reachability *r = [Reachability reachabilityWithHostName:@www.maxqdata.com]; NetworkStatus internetStatus = [r
Consider the following scenario: http://www.yourdomain.com/Default.aspx?p=2 Now we ofcourse want to check if the querystring
After reading the MSDN article ( http://msdn.microsoft.com/en-us/magazine/2009.01.genevests.aspx ) on implementing a Custom STS using
Consider the following scenario: http://www.restserver.com/example.php returns some content that I want to work with

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.