I came upon this piece of C# code that uses delegates and passes the function to the delegate by reference...
delegate bool MyDel(int x);
static bool fun(int x) {
return x < 0;
}
public static void Main() {
var d1 = new MyDel(fun); // what I usually write
var d2 = new MyDel(ref fun);
}
The compiler didn't complain and built the project fine. I didn't find any difference while running some test cases, does this syntax make any difference than the usual syntax?
As InBetween mentioned, it seems that this syntax is also valid (and it makes even less sense)
var d3 = new MyDel(out fun);
it seems, in this case, there is no difference passing by ref / out / normal-way, I tried to see if any difference in ILDASM but no difference...
delegate void MyDel();
static void Main(string[] args)
{
MyDel myDel = new MyDel(Mtd1);
MyDel d3 = new MyDel(ref Mtd1);
MyDel d1 = new MyDel(ref Mtd2);
MyDel d2 = new MyDel(out Mtd3);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With