0

How does object reference work in c# - I tried this, I am currently trying have an object being an reference to another object instance.

I need this in a class I am building

    public class TransactionalProducer 
    {
        private readonly IMessageBusProducer producer;

        public TransactionalProducer (IMessageBusProducer bus, bool hasTransactionInitialized)
        {
            producer = bus;
            if(hasTransactionInitialized)
            {
                producer.InitTransactions();
            }
            producer.BeginTransaction();
        }
  }

It looks like when I create a new TransactionalProducer the producer instance will be set as an new instance, where the values is taken from the function parameter bus, and not be reference to the one being parsed via the constructor.

How do I make sure that when an new TransactionalProducer object is being instantiated via the constructor public TransactionalProducer (IMessageBusProducer bus, bool hasTransactionInitialized) is the producer set as an object reference to bus, and not create a new IMessageBusProducer with the same parameters as function param bus

noob
  • 19
  • 1
  • 2
    a,b,c are not references. They are variables that hold references. By saying c = b you copy reference value from b to c. But then you overwrite b with reference to another string. – Wiktor Zychla Aug 14 '21 at 20:17
  • Check out my answer to this question https://stackoverflow.com/questions/65418824/object-passing-reference-vs-value. Your three literal strings are _objects_. Your variables hold references to those objects. When you do assignment between two variables of reference type, the reference that the right hand variable holds is assigned to the variable on the left. When you say `c=b`, `c` ends up referring to the same object `b` does. When you reassign `b`, `c` doesn't care. Think of reference type variables as acting like value type variables whose values are references. – Flydog57 Aug 14 '21 at 20:26
  • Also, ignoring the fact that strings are immutable (which they are), assignment like you are doing _does not_ mutate the object to which the right hand variable refers. At the end of `c=b`, no objects are changed. Only the variable only the left side changes (a copy of the reference stored I the variable on the right side is copied over) – Flydog57 Aug 14 '21 at 20:37
  • so it is not possible c# to do so? @Flydog57 – noob Aug 14 '21 at 20:50
  • 1
    "To do so" what? You can pass a reference by reference as a parameter to a function (see my answer to the question I mention above). But, you can't do _by reference_ assignment of reference type variables. Why do you think you want to do this? What purpose are you trying to achieve? – Flydog57 Aug 14 '21 at 21:19
  • @noob, please do not update your question so drastically. Previously given answers and advice now no longer make any sense. Please rollback your question to how you originally had it and instead either clarify further or ask a more focused question in a new post. – Daniel Aug 15 '21 at 04:14
  • producer will be a reference to the passed in bus object so whatever is done to producer will, or at least should, reflect in the bus object. Is this not what you are experiencing? What evidence are you basing this assumption off of? – Daniel Aug 15 '21 at 04:19

1 Answers1

2

String objects are immutable: they cannot be changed after they have been created. b = "let me rewrite this" created a new object in a hip and assign the address of this object to b. c is still having the addres of an old object.

If you want to bind two variables try to use references.

  string b = "This is changed";
    ref string c =  ref b;
    Console.WriteLine(c);
    b = "let me rewrite this";
    Console.WriteLine(c);
    b = "let me rewrite this again";
    Console.WriteLine(c);
    c = "let me reverse";
    Console.WriteLine(b);
    Console.WriteLine(c);

output

This is changed
let me rewrite this
let me rewrite this again
let me reverse
let me reverse

I don't understand what are trying to reach but try this, it could be working for you.

public TransactionalProducer (ref IMessageBusProducer bus, bool hasTransactionInitialized)

it will reach your inside busproducer, but I am not sure if it can cause a memory leak.

Serge
  • 40,935
  • 4
  • 18
  • 45
  • so what I am doing with the class itself should use references correctly? – noob Aug 14 '21 at 20:15
  • @noob I don't know what you need. Try to use references. Check my answer update. – Serge Aug 14 '21 at 21:14
  • OK, I stand corrected. In very modern C#, you can do _by reference_ assignment of reference type variables. I'm not looking forward to my first code review of code that includes this feature – Flydog57 Aug 14 '21 at 21:21
  • @Serge: upvoted, however recommending references to references to someone who doesn't quite get references, could not be the best idea ;) – Wiktor Zychla Aug 15 '21 at 19:19
  • @WiktorZychla Thank you for your feedback. Yes, I am agree with you. I remember good old days when I was using classic C. I still sometimes have nightmares of pointers to pointers. – Serge Aug 15 '21 at 19:35