syntax - How to provide default value for a parameter of delegate type in C#? -
In C #, we can provide the default values of parameters such as:
void Foo ({}
However, when the method is signed:
zero FooWithDelegateParam (function & lt; string, string> prediction) {} < How can we pass the default parameter: zero FooWithDelegateParam (Func> string, string> predicate = (string ) = & Gt; {return Y;}) {}
but do not compile it So, what is the correct syntax for doing this?
Note: I provide a way to assign a input string to output string mapper through a delegate I am trying, and if not provided, I just want to return the input string. Therefore, suggestions on any alternative approach to getting it are greatly appreciated thanks.
The default values for the parameters you can not basically have are the compress-time constants. However, if you are happy to use the value of the value as "code default", then it may be:
zero FooWithDelegateParam (Funk & lt; string, string & gt; = null) {predicate = predicate ?? (X = & gt; x); Pros and Cons:
- > The overload solution works with languages which are optional parameters (eg C # Before 4.0) do not support
- The overload solution distinguishes between
null
and "default". It is professional and opposed in its own right: - If the caller should not give any
null
value, then the overloaded version can find insects, where it accidentally This is doing - If you do not believe that there will be any such bug, then the optional parameter version allows you to represent "default" in the code - you can type "
null >
Optional Parameter Edition Interface It's easy to express ... - ... with the negative side that the default value will also need to be expressed in the implementation (this is somewhat normal for the overload solution, in both cases In this, the intuitive implementation of the interface can be defaults by using the template pattern pattern.)
Comments
Post a Comment