When adding to a list, if I add the property of a class instance, is it set to a reference of the property’s value or a reference to the property?
Example:
If I have:
class A {
public Action<Guid> SomeDelegate { get; set; }
}
And in a different class I create an instance of class A, ie:
class B {
public B() {
a = new A();
a.SomeDelegate = someFunction;
List<Action<Guid>> myList = new List<Action<Guid>>;
myList.Add(a.SomeDelegate);
a.SomeDelegate = anotherFunction;
}
}
What will be in myList? A reference to anotherFunction or a reference to someFunction?
If it is a reference to someFunction, how would I go about making it a reference to anotherFunction?
Thanks!
I believe the List would continue to hold a reference to a delegate instance pointing to SomeFunction.
The low-tech solution to achieving what you want to achieve would be to wrap the delegate inside another object. Add this object to the list; you are then free to mutate the wrapper object, change its property to the new value.
At the point where you consume the delegates in the list, instead of directly invoking the delegate, use
wrapperInstance.Delegate(params)Update: I think my response wasn’t clear from Jason’s comment.