Can a Delegate have an optional parameter?

前端 未结 2 1021
抹茶落季
抹茶落季 2020-12-06 11:17

I have the below code that was working fine until I tried adding the bool NetworkAvailable = true portion. Now I get a Method name expected compil

2条回答
  •  半阙折子戏
    2020-12-06 11:40

    To some very limited extent. Using C# 4 :

     public delegate void Test(int a, int b = 0);
    
     static void T1(int a, int b) { }
     static void T2(int a, int b = 0) { }
     static void T3(int a) { }
    
    
        Test t1 = T1;
        Test t2 = T2;
        Test t3 = T3;   // Error
    

    And then you can call

        t1(1);
        t1(1, 2);
        t2(2);
        t2(2, 3);
    

提交回复
热议问题