How to provide default value for a parameter of delegate type in C#?

本小妞迷上赌 提交于 2019-11-30 03:03:53

问题


In C# we can provide default value of the parameters as such:

void Foo(int i =0) {}

But, when the method signature is:

void FooWithDelegateParam(Func<string,string> predicate)
{}

How can we pass the default parameter:

void FooWithDelegateParam(Func<string,string> predicate = (string,string x)=> {return y;})
{}

But this won't compile. So, what is the proper syntax for doing so ?

Note: I'm trying to provide a way to specify an input-string to output-string mapper through a delegate, and if not provided I simply want to return the input string. So, suggestions on any alternative approach to achieve this is highly appreciated as well. Thanks.


回答1:


You can't, basically. Default values for parameters have to be compile-time constants. However, if you're happy to use null as a value meaning "use the default" you could have:

void FooWithDelegateParam(Func<string, string> predicate = null)
{
    predicate = predicate ?? (x => x);
    // Code using predicate
}

Or use an overload, as per Alireza's suggestion, of course.

Each option has different implications:

  • The overload solution works with languages which don't support optional parameters (e.g. C# before 4.0)
  • The overload solution differentiates between null and "the default". This in itself has pros and cons:
    • If the caller should never provide a null value, the overload version can find bugs where it's accidentally doing so
    • If you don't believe there will be any such bugs, the optional parameter version allows the idea of "the default" to be represented in code - you could pass a "null meaning default" value through multiple layers, letting only the bottom-most layer determine what that default actually means, and do so more simply than having to explicitly call different overloads
  • The optional parameter version is simpler to express in an interface...
    • ... with the downside that the default value would still need to be expressed in the implementation. (This is somewhat common to the overload solution, mind you... in both cases, an abstract class implementing the interface could do the defaulting using the template method pattern.)



回答2:


You can't specify default value like that. Write an overload instead:

void FooWithDelegateParam()
{
    FooWithDelegateParam(s => s);
}

void FooWithDelegateParam(Func<string,string> predicate)
{

}


来源:https://stackoverflow.com/questions/24978661/how-to-provide-default-value-for-a-parameter-of-delegate-type-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!