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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:40:53+00:00 2026-05-13T17:40:53+00:00

Say, I have a class: class M { public int val; And also a

  • 0

Say, I have a class:

class M
{
     public int val; 

And also a + operator inside it:

    public static M operator +(M a, M b)
    {
        M c = new M();
        c.val = a.val + b.val;
        return c;
    }
}

And I’ve got a List of the objects of the class:

List<M> ms = new List();
M obj = new M();
obj.val = 5;
ms.Add(obj);

Some other object:

M addie = new M();
addie.val = 3;

I can do this:

ms[0] += addie;

and it surely works as I expect – the value in the list is changed. But if I want to do

M fromList = ms[0];
fromList += addie;

it doesn’t change the value in ms for obvious reasons.

But intuitively I expect ms[0] to also change after that. Really, I pick the object from the list and then I increase it’s value with some other object. So, since I held a reference to ms[0] in fromList before addition, I want still to hold it in fromList after performing it.

Are there any ways to achieve that?

  • 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-13T17:40:53+00:00Added an answer on May 13, 2026 at 5:40 pm

    You shouldn’t expect ms[0] to change. After it’s been initialized ,fromList isn’t connected with ms at all – fromList and ms[0] happen to have the same value, but that’s all. += is returning a new value (as indeed it should) so you’re just changing the value stored in fromList, which is completely independent of the value in the list. (Note that the values here are references, not objects.)

    Don’t try to change this behaviour – it’s doing the right thing. Change your expectations instead.

    If you really want the contents of the list to reflect the change, you either need to change the value in the list to refer to the new object, or you need to change your code to mutate the existing object instead of creating a new one. If you take the latter approach you should not do this within an operator. Instead, create an Add method, so you’d call:

    fromList.Add(addie);
    

    It’s fairly clear that that’s a mutating operation, so it won’t break any expectations.

    Personally I’d try to use immutable types instead, and adjust your design so that you don’t need the list to change (or you operate on the list directly).

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

Sidebar

Related Questions

say I have: class Test { public static int Hello = 5; } This
Say I have a class: public class R { public static final int _1st
let say i have following class: class Shape { public int widht; public List<Point>
Say I have this class: public class Account { public int AccountID { get;
Say I have this class: class myclass { public int Field1{ get; set; }
Let's say I have a CategoryKey class: public class CategoryKey { public int layer;
Say I have a simple object such as class Something { public int SomeInt
Lets say i have this usercontrol public class test : UserControl { public int
Lets say have this immutable record type: public class Record { public Record(int x,
Say you have two classes, order and customer: public class Customer{ public int CustomerId

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.