Parameter Action in which T3 can be optional

后端 未结 3 611
再見小時候
再見小時候 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:24

    Hoping to help others with what I find as being a more elegant implementation of overloading mixed with the (delegate-oriented) strategy pattern.

    public class OverloadExample {
        private Action _implementation;
    
        public OverloadExample() {
            _implementation = defaultImplementation;
        }
    
        public OverloadExample(Action implementation) {
            _implementation = implementation;
        }
    
        protected void defaultImplementation(int aInt, bool aBool) {
            //
        }
    
        public void Implementation(int someInt, bool someBool = true) {
            _implementation(someInt, someBool);
        }
    }
    

    Usage:

    new OverloadExample().Implementation(9001);
    new OverloadExample().Implementation(9001, false);
    

提交回复
热议问题