I have two objects, from same class type. They both have a property (and its private field). Conditionally, I would like to assign this property/field as a reference to the same property/field of another instance of the same class.
It's important to note that my requirement is to read the referenced field. Write to it would be a bonus, not a problem, and not necessary.
I would like to know if it would be possible to use a ref
field, and do some logic inside the property getter.
Also, I know there has been some questions about this here on StackOverflow (here, here, here, here), but they are all pretty old (so the idea of duplicate does not seem to apply here).
Now we are on C# 10.0, and a lot of different ref
capabilities had been implemented, like ref locals. I tried to use some of them, but I failed. Hope someone with more knowledge could help me to see a way to do it.
So, is it possible, without using a wrapper to link one instance's property to another?? (which is my current approach, detailed below)
More details:
That's my class (actually it's a huge simplification of it):
public class Person
{
private string _fullName;
public string FullName { get => _fullName; set => _fullName = value; }
}
And, let's say I have 3 instances of this class. Now, if I try to assign the value of one property to another, the value is simply copied (as expected. I already am fairly aware why it is):
var person01 = new Person() { FullName = "Steve" };
var person02 = new Person() { FullName = "Mr. S" };
var person03 = new Person() { FullName = "Mister Steve" };
person02.FullName = person01.FullName;
// person02.FullName is "Steve"
person01.FullName = "Mr. Steve Jobs";
// person01.FullName is "Mr. Steve Jobs"
// person02.FullName is "Steve" because the value was only copied.
I UNDERSTAND WHY IT IS. But I want to know if there is some elegant way to do this:
ref person02.FullName = ref person01.FullName; // not allowed
ref person03.FullName = ref person02.FullName; // not allowed
person01.FullName = "Mr. Steve Jobs";
// person01.FullName is "Mr. Steve Jobs"
// person02.FullName is "Mr. Steve Jobs"
// person03.FullName is "Mr. Steve Jobs"
So the idea is assign a field as a reference to another field of another object. I believe that through a Property it's not possible, since it's actually a "method" to get and set the private field.
So i did this, it works, but it's not pretty:
public class Person
{
private string _fullName;
public string FullName
{
get => (FullName_Reference == null ? _fullName : FullName_Reference.FullName);
set => _fullName = value;
}
public Person FullName_Reference { get; set; }
public Person()
{
_fullName = "";
FullName_Reference = null;
}
}
var person01 = new Person() { FullName = "Steve" };
var person02 = new Person() { FullName = "Mr. S" };
var person03 = new Person() { FullName = "Mister Steve" };
person02.FullName_Reference = person01;
person03.FullName_Reference = person02;
person01.FullName = "Mr. Steve Jobs";
// person01.FullName is "Mr. Steve Jobs"
// person02.FullName is "Mr. Steve Jobs"
// person03.FullName is "Mr. Steve Jobs"
Seeking for a more elegant way to do it, I even tried to expose the private field _fullName
(through a method returning a ref
), but i was unable to set the private field (also through a method) of the other object as a reference to the reference object returned previously. It always just copy its value.