C# csharp项目中方法之间的不明确性

C# csharp项目中方法之间的不明确性,c#,C#,这是主要课程: public class ReportsParameter { public ReportsParameter(string p, decimal? nullable); public ReportsParameter(string ParameterName, string Value); public string parameterName { get; set; } public string value { get; set; } }

这是主要课程:

public class ReportsParameter
{
    public ReportsParameter(string p, decimal? nullable);
    public ReportsParameter(string ParameterName, string Value);

    public string parameterName { get; set; }
    public string value { get; set; }
}
在另一类中,我使用:

reportsParameters1.Add(new ReportsParameter("Title", txtTitle.Text));
reportsParameters.Add(new ReportsParameter("IsCurrency", null));
reportsParameters.Add(new ReportsParameter("IsInactive", null));
当我构建项目时,出现以下错误:

以下方法或属性之间的调用不明确: “General.ReportsParameter.ReportsParameter(字符串,字符串)”和 '常规.ReportsParameter.ReportsParameter(字符串,十进制?)'

包含
IsCurrency
IsInactive
的两行出现错误


我可以使用
DBNULL.Value.Tostring()
?或者null不同于dbnull?

,这是因为具有可为空的
十进制数?
和也可为空的
字符串

试着这样做。。指示编译器要调用的适当重载

reportsParameters.Add(new ReportsParameter("IsCurrency", (string)null));


根据需要

要添加到@bit的答案中,您可以将您的
ReportsParameter
类设置为通用类,这取决于您以后如何使用它。这样做的好处是它不会丢失原始参数的类型,这意味着您可以获得更强的类型安全性。如果未指定任何值,您还可以为
参数提供默认的
null
值:

public class ReportsParameter<T>
{
    public ReportsParameter(string name, T value = default(T))
    {
        _name = name; _value = value;
    }

    private readonly string _name;
    public string Name 
    { get { return _name; } }

    private readonly T _value;
    public T Value 
    { get { return _value; } }
}
公共类报表参数表
{
公共报表参数(字符串名称,T值=默认值(T))
{
_名称=名称;_值=值;
}
私有只读字符串\u名称;
公共字符串名
{get{return{u name;}}
私有只读T_值;
公共价值
{获取{返回_值;}}
}
然后在实例化时传递泛型参数:

// no need to pass the second parameter if it's null
list.Add(new ReportsParameter<string>("IsCurrency"));
list.Add(new ReportsParameter<double?>("IsInactive"));
//如果第二个参数为null,则无需传递
添加(新报告参数(“IsCurrency”);
添加(新报告参数(“IsInactive”);
一个缺点是,要从编译器获得类型推断,还需要一个工厂方法,如:

public static class Reports
{
    public ReportsParameter<T> CreateParameter<T>
        (string name, T value = default(T))
    {
        return new ReportsParameter<T>(name, value);
    }
}
公共静态类报告
{
公共报表参数创建参数
(字符串名称,T值=默认值(T))
{
返回新的ReportsParameter(名称、值);
}
}
它允许您在可以推断类型时跳过显式泛型参数:

// no need to specify <string> in this case
reportsParameters1.Add(Reports.CreateParameter("Title", txtTitle.Text));

// but you need to do it here to resolve ambiguity
// (no need to pass the default value, however)
reportsParameters.Add(Reports.CreateParameter<string>("IsCurrency"));
reportsParameters.Add(Reports.CreateParameter<decimal?>("IsInactive"));
//在这种情况下无需指定
reportsParameters1.Add(Reports.CreateParameter(“Title”,txtTitle.Text));
//但你需要在这里解决歧义
//(但是,无需传递默认值)
reportsParameters.Add(Reports.CreateParameter(“IsCurrency”));
reportsParameters.Add(Reports.CreateParameter(“IsInactive”);

首先创建一个变量并将其分配给参数如何?为什么不使小数不可为空?然后,您可以公开另一个不接受decimal的构造函数重载,并在内部将decimal设置为null。这对用户来说更好,因为他们不需要显式地为decimal参数传递null。您认为应该调用哪个方法?
// no need to specify <string> in this case
reportsParameters1.Add(Reports.CreateParameter("Title", txtTitle.Text));

// but you need to do it here to resolve ambiguity
// (no need to pass the default value, however)
reportsParameters.Add(Reports.CreateParameter<string>("IsCurrency"));
reportsParameters.Add(Reports.CreateParameter<decimal?>("IsInactive"));