【问题标题】:How can I set default parameter value for an action如何为操作设置默认参数值
【发布时间】:2013-10-07 07:09:44
【问题描述】:

当参数是字典类型时,如何为操作设置默认参数值?

例如:

public void Test(Dictionary<int, bool> dic) {
    ...
}

【问题讨论】:

  • 该方法签名无效。你想达到什么目的?

标签: c# methods parameters default-value


【解决方案1】:

你不能给它一个你想要的默认值,因为它必须是一个编译时间常数值,但你可以这样做:

private static bool Test(Dictionary<string, string> par = null)
    {
        if(par == null) par = GetMyDefaultValue();
        // Custom logic here
        return false;
    }

【讨论】:

    【解决方案2】:

    您可以使用null 作为特殊情况,就像在其他答案中一样,但是如果您仍然希望能够调用Test(null) 并且具有与调用Test() 不同的行为,那么您必须链接重载:

    public void Test(Dictionary<int, bool> dic) {
      //optional, stops people calling Test(null) where you want them to call Test():
      if(dic == null) throw new ArgumentNullException("dic");
    ...
    }
    
    public void Test() {
       var defaultDic = new Dictionary<int, bool>();
       Test(defaultDic);
    }
    

    【讨论】:

    • 这是最好的解决方案。另外,Test() 应该是 sealed
    • 此外,如果您使用 null,您不知道调用者是否也使用了 null,您可能希望捕获这种情况并抛出异常,以使 API 更干净。跨度>
    • @AdamHouldsworth 如果您想拨打Test(null) 并且与拨打Test() 有不同的结果,这是必须的
    • @AdamHouldsworth 已经明确了。干杯:)
    • @AdamHouldsworth @weston 对不起,伙计们。我最近也做了很多java。
    【解决方案3】:

    您只能使用null 作为引用类型的默认参数值。

    默认值必须是以下表达式类型之一:

    • 一个常量表达式;

    • new ValType() 形式的表达式,其中ValType 是值类型,例如enumstruct

    • default(ValType) 形式的表达式,其中ValType 是值类型。

    MSDN

    【讨论】:

    • 他可以,如果项目是null,可以提供默认的null 和一些合理的默认值。
    • 是的,除非null 是一个有效的参数值。
    • 我知道你来自哪里,基本上他无法在方法签名中提供他想要的确切默认值。不幸的是,您的回答非常明确,并没有说明 null 实际上对参考参数有效。
    • a constant expression;null 是一个常量表达式;所以我明确地这么说。
    • 是的,如果刚接触该语言的人知道null 是一个常量表达式并且对Dictionary&lt;int, bool&gt; 有效,那么这一切都紧跟在“你不能”之前,所以我仍然觉得它是不清楚。
    【解决方案4】:

    您不能将字典设置为 NULL 以外的任何值。如果您确实尝试,例如:

    public void Test(Dictionary&lt;int, bool&gt; dic = new Dictionary&lt;string, string&gt; { { "1", "true" }}) 或者其他什么,那么你会看到这个错误:

    “dic”的默认参数值必须是编译时常量。

    所以在这种情况下,NULL 是您唯一的选择。但是,这样做是没有意义的

    public void Test(Dictionary&lt;int, bool&gt; dic = null)

    如果调用者没有实例化新实例,最坏的情况是传入的dic 无论如何都会是NULL,因此无论如何添加NULL 默认值没有任何好处。

    【讨论】:

      【解决方案5】:

      假设您想在方法签名中提供非null 默认值,您无法使用此类型执行此操作。但是,您有两种替代解决方案 立即浮现在脑海中。

      1,使用带有默认值的可选参数,对于Dictionary,这必须是null(我相信除了string之外的所有其他引用类型),您需要方法内部的逻辑来处理它:

      public void Test(Dictionary<int, bool> dictionary = null)
      {
          // Provide a default if null.
          if (dictionary == null)
              dictionary = new Dictionary<int, bool>();
      }
      

      或者,我会这样做,只是使用“老式”方法重载。这使您可以区分不提供参数的人和提供null 参数的人:

      public void Test()
      { 
          // Provide your default value here.
          Test(new Dictionary<int, bool>();
      }
      
      public void Test(Dictionary<int, bool> dictionary)
      {
      
      }
      

      可选参数无论如何都会编译成重载的方法,所以它们几乎 语义相同,只是您希望在哪里表达默认值的偏好。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-04
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 2011-07-31
      • 2019-03-07
      • 1970-01-01
      • 2013-06-01
      相关资源
      最近更新 更多