如何在 C# 中为委托类型的参数提供默认值?

How to provide default value for a parameter of delegate type in C#?(如何在 C# 中为委托类型的参数提供默认值?)
本文介绍了如何在 C# 中为委托类型的参数提供默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C# 中,我们可以提供参数的默认值,如下所示:

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

void Foo(int i =0) {}

但是,当方法签名是:

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.

推荐答案

你不能,基本上.参数的默认值必须是编译时常量.但是,如果您乐于使用 null 作为表示使用默认值"的值,您可以:

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
}

当然,也可以按照 Alireza 的建议使用重载.

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

每个选项都有不同的含义:

Each option has different implications:

  • 重载解决方案适用于不支持可选参数的语言(例如 4.0 之前的 C#)
  • 重载解决方案区分 null 和默认值".这本身就有利有弊:
    • 如果调用者永远不应该提供 null 值,则重载版本可以找到它意外这样做的错误
    • 如果您不相信会有任何此类错误,可选参数版本允许在代码中表示默认"的想法 - 您可以传递null 表示默认"通过多个层传递值,只让最底层确定默认值的实际含义,这样做比必须显式调用不同的重载更简单
    • 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
      • ... 缺点是仍然需要在实现中表示默认值.(这在重载解决方案中有些常见,请注意……在这两种情况下,实现接口的抽象类都可以使用模板方法模式进行默认设置.)

      这篇关于如何在 C# 中为委托类型的参数提供默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)