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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:35:14+00:00 2026-05-11T22:35:14+00:00

What is the proper way to implement assignment by value for a reference type?

  • 0

What is the proper way to implement assignment by value for a reference type? I want to perform an assignment, but not change the reference.

Here is what I’m talking about:

void Main()
{
    A a1 = new A(1);
    A a2 = new A(2);
    a1 = a2; //WRONG: Changes reference
    a1.ValueAssign(a2); //This works, but is it the best way?
}

class A
{
    int i;

    public A(int i)
    {
        this.i = i;
    }

    public void ValueAssign(A a)
    {
        this.i = a.i;
    }
}

Is there some sort of convention I should be using for this? I feel like I’m not the first person that has encountered this. Thanks.

EDIT:

Wow. I think I need to tailor my question more toward the actual problem I’m facing. I’m getting a lot of answers that do not meet the requirement of not changing the reference. Cloning is not the issue here. The problem lies in ASSIGNING the clone.

I have many classes that depend on A – they all share a reference to the same object of class A. So, whenever one classes changes A, it’s reflected in the others, right? That’s all fine and well until one of the classes tries to do this:

myA = new A();

In reality I’m not doing new A() but I’m actually retrieving a serialized version of A off the hard drive. But anyways, doing this causes myA to receive a NEW REFERENCE. It no longer shares the same A as the rest of the classes that depend on A. This is the problem that I am trying to address. I want all classes that have the instance of A to be affected by the line of code above.

I hope this clarifies my question. Thank you.

  • 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-11T22:35:14+00:00Added an answer on May 11, 2026 at 10:35 pm

    I wish there was a “second best” answer option, because anyone who mentioned Observer deserves it. The observer pattern would work, however it is not necessary and in my opinion, is overkill.

    If multiple objects need to maintain a reference to the same object (“MyClass”, below) and you need to perform an assignment to the referenced object (“MyClass”), the easiest way to handle it is to create a ValueAssign function as follows:

    public class MyClass
    {
        private int a;
        private int b;
    
        void ValueAssign(MyClass mo)
        {
            this.a = mo.a;
            this.b = mo.b;
        }
    }
    

    Observer would only be necessary if other action was required by the dependent objects at the time of assignment. If you wish to only maintain the reference, this method is adequate. This example here is the same as the example that I proposed in my question, but I feel that it better emphasizes my intent.

    Thank you for all your answers. I seriously considered all of them.

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

Sidebar

Ask A Question

Stats

  • Questions 168k
  • Answers 168k
  • 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 RGB means to ensure that you save the image as… May 12, 2026 at 1:45 pm
  • Editorial Team
    Editorial Team added an answer You're on the right track. Here's the problems I saw:… May 12, 2026 at 1:45 pm
  • Editorial Team
    Editorial Team added an answer You may want to take a look at TableAdapter Overview… May 12, 2026 at 1:45 pm

Related Questions

What is the correct way to implement and architect a command line tool as
I'm using Smarty and utilizing the config_load respective {#VAR#} variables to implement localization. This
In C#, is it possible to have an object that has multiple method signatures
I have a login system requiring a username and a password. I want to

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.