Parameter Action in which T3 can be optional

后端 未结 3 609
再見小時候
再見小時候 2020-11-30 08:52

I have the following code:

public static MyMethod()  
{ 
   ...Do something  
   ProtectedMethod(param1, param2);  
   ...Do something  
}  

protected stati         


        
3条回答
  •  一个人的身影
    2020-11-30 09:21

    Optional parameters are an attribute of a method or delegate parameter. When you call a signature (method or delegate) that has a known optional parameter at compile-time, the compiler will insert the optional parameter value at the callsite.

    The runtime is not aware of optional parameters, so you can't make a delegate that inserts an optional parameter when it's called.

    Instead, you need to declare a custom delegate type with an optional parameter:

    public delegate void MyDelegate(IEnumerable param1, string param2, int param3 = 1);
    

    When calling this delegate, you will be able to omit the third parameter, regardless of the declaration of the method(s) it contains.

提交回复
热议问题