C
C#2y ago
Xan.Nava

Combining func reference behavior [Answered]

If i combine func3 += func1 and func3 += func2 and then lets say func1 += someMethod Will func3 also have it added?
10 Replies
mtreit
mtreit2y ago
Well, not directly But indirectly yes Like if you removed func1 from the chain subsequently then invoking func3 will not result in a call to someMethod
Xan.Nava
Xan.Nava2y ago
Exactly what I was hoping for Though if you did func3 -= someMethod would it be removed from func1 also?
333fred
333fred2y ago
No
Xan.Nava
Xan.Nava2y ago
Is it is more like sets. The func3 set has two sets in it func1 and func2, and then func1 is a set with someMethod in it? So removing someMethod from func3 wouldn't work because it's set is just func1 and func2 but calling func3 will call everything in its sub sets?
MODiX
MODiX2y ago
Orannis#3333
REPL Result: Success
var func1 = () => Console.WriteLine("Func1");
var func2 = () => Console.WriteLine("Func2");
var func3 = () => Console.WriteLine("Func3");
var combined1and2 = func1 + func2;
var combinedAll3 = func3 + combined1and2;
combined1and2 -= func2;
combinedAll3();
var func1 = () => Console.WriteLine("Func1");
var func2 = () => Console.WriteLine("Func2");
var func3 = () => Console.WriteLine("Func3");
var combined1and2 = func1 + func2;
var combinedAll3 = func3 + combined1and2;
combined1and2 -= func2;
combinedAll3();
Console Output
Func3
Func1
Func2
Func3
Func1
Func2
Compile: 629.132ms | Execution: 69.840ms | React with ❌ to remove this embed.
mtreit
mtreit2y ago
Yes, think of it as a graph
333fred
333fred2y ago
See how modifying combined1and2 didn't affect combinedAll3?
Xan.Nava
Xan.Nava2y ago
ah yeah
333fred
333fred2y ago
I'm going to assume this is done
Accord
Accord2y ago
✅ This post has been marked as answered!