Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between passing a function to a delegate by ref?

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?

Update

As InBetween mentioned, it seems that this syntax is also valid (and it makes even less sense)

var d3 = new MyDel(out fun);
like image 563
Ayman El Temsahi Avatar asked Jan 27 '17 07:01

Ayman El Temsahi


Video Answer


1 Answers

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);
    }

enter image description here

like image 175
AKN Avatar answered Oct 19 '22 05:10

AKN