【问题标题】:How to pass a class as optional parameter to a method?如何将类作为可选参数传递给方法?
【发布时间】:2020-04-07 13:13:52
【问题描述】:

我在这里搜索过类似的问题,但找不到解决问题的方法。 MyClass 保存多个数据并在不同类型之间进行一些类型转换。

我怎样才能避免这个错误:

“字符串”类型的值不能用作默认参数 因为没有标准转换类型 Program.MyClass?

我已经尝试过 Func 并声明了多个函数重载,以便能够传递多个参数并处理默认参数。应该有更好的方法来实现这一点。希望你能帮忙。

为了澄清我的问题,我编写了以下代码:

using System;

public class Program
{
    public class MyClass {
            public string Value { get; set; } 
            public MyClass()
            {
                Value = "";
            }
            public MyClass(string s)
            {
                Value = s;
            }
            public override string ToString()
            {
                return this.Value;
            }
    }

    // Causes CS1750
    // A value of type 'string' cannot be used as default parameter
    // because there are no standard conversions to type 'Program.MyClass'
    public static string test2(string a, MyClass b = " with default text")
    {
       return a + b;
    }

    public static string test(string a, string b = " with default text")
    {
       return a + b;
    }

    public static void Main()
    {
        Console.WriteLine(test("test1"));
        Console.WriteLine(test("test1", " with my text"));
    }
}

【问题讨论】:

  • 您不能将字符串分配给类的对象...为什么要这样做?类类型的参数不能有默认参数值。
  • 您可能想为您的班级添加implicit conversion operator:public static explicit operator MyClass(string s)

标签: c# .net type-conversion


【解决方案1】:

这不太可能。如错误消息所述,您需要进行标准转换:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/conversions#standard-conversions

我们所能做的就是为类定义一个隐式转换运算符:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/user-defined-conversion-operators

在你的情况下,它会是这样的:

public static implicit operator MyClass(string val) => new MyClass(val);

然后用类似这样的东西替换你的测试方法:

public static string test(string a, MyClass b = null)
{
    b = b ?? " with default text";
    return a + b;
}

另请注意,如果只提供一个参数,则两个测试方法具有相同的签名,因此编译器将不知道要使用哪一个,请删除第二个测试方法的默认值。

【讨论】:

    【解决方案2】:

    使用null作为默认值,并将其替换为方法内部的默认值:

    public static string test2(string a, MyClass b = null)
    {
       if (b == null) return a + " with default text";
       return a + b.Value;
    }
    

    【讨论】:

      【解决方案3】:

      在string 和MyClass 之间添加一个隐式转换运算符

      public class MyClass 
      {
              public string Value { get; set; } 
              public MyClass() : this(string.Empty) { }
              public MyClass(string s)
              {
                  Value = s;
              }
              public override string ToString() => this.Value;
              public static implicit operator string(MyClass item) => item.Value;
              public static implicit operator MyClass(string value) => new MyClass(value)l;
      }
      

      【讨论】:

        【解决方案4】:

        你应该改变2个步骤(如果申请全班)

        1.构造函数的初始默认值

        public MyClass()
        {
            Value = " with default text";
        }
        

        2.更改方法的默认参数

        public static string test(string a, MyClass b)
        {
           return a + b;
        }
        

        顺便说一句,This post 对你有帮助。


        更新:仅在函数内设置默认值

        上面的代码只适合申请全班的情况。 如果你想只在函数内设置默认值,你可以试试下面的代码

        public static string test2(string a, MyClass b = null)
        {
           var defaultValue = " with default text";
           return a + (b == null ? defaultValue : b.Value);
        }
        

        【讨论】:

        • 这会为整个类设置默认值,而不是只在函数内部。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-26
        • 2018-03-02
        • 2018-12-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多