C#:如果输入为空,如何设置默认值

C#:如果输入为空,如何设置默认值,c#,methods,C#,Methods,如果我有这样的方法: public void AddSomething(string ice = "10", string sweet = "20") { Console.Write(ice); Console.Write(sweet); } public void AddSomething(string ice = "10", string sweet = "20") { if(ice = "") ice = default_vaule; //w

如果我有这样的方法:

public void AddSomething(string ice = "10", string sweet = "20")
{
    Console.Write(ice);
    Console.Write(sweet);
}
public void AddSomething(string ice = "10", string sweet = "20")
{
    if(ice = "")
        ice = default_vaule;    //which is 10
    if(sweet = "")
        sweet = default_vaule;  //which is 20
    Console.Write(ice);
    Console.Write(sweet);
}
因此,如果我输入一个字符串,它将写入该字符串。如果没有,它将写入默认字符串
(10,20)

但我想要这样的东西:

public void AddSomething(string ice = "10", string sweet = "20")
{
    Console.Write(ice);
    Console.Write(sweet);
}
public void AddSomething(string ice = "10", string sweet = "20")
{
    if(ice = "")
        ice = default_vaule;    //which is 10
    if(sweet = "")
        sweet = default_vaule;  //which is 20
    Console.Write(ice);
    Console.Write(sweet);
}
因此,如果用户输入一个空字符串
,我可以将默认值写入用户,这样我不仅可以执行以下操作:

AddSomething("5");
此外,这两项:

AddSomething("5","");
AddSomething("","5");

有人知道怎么做吗?谢谢

你已经回答了你的问题。您还可以覆盖空案例

public void AddSomething(string ice = "10", string sweet = "20")
{
    if(string.IsNullOrEmpty(ice)) ice = "10";
    if(string.IsNullOrEmpty(sweet)) sweet = "20";
    Console.Write(ice);
    Console.Write(sweet);
}
如果不想写入重复的文本,可以使用常量

// these are constant and can be used as default value for parameters.
const string DefaultSweet = "20";     
const string DefaultIce = "10";

public void AddSomething(string ice = DefaultSweet, string sweet = DefaultIce)
{
    if(string.IsNullOrEmpty(ice)) ice = DefaultIce;
    if(string.IsNullOrEmpty(sweet)) sweet = DefaultSweet;
    Console.Write(ice);
    Console.Write(sweet);
}
旁注:
string.IsNullOrEmpty(ice)
相当于
ice==“”| | ice==null
,这个问题(至少对我来说)不清楚,但根据你的帖子,我可以建议这样的解决方案:

// default value for "ice"
const string default_ice = "10";
// default value for "sweet"
const string default_sweet = "20";

public void AddSomething(string ice = default_ice, string sweet = default_sweet)
{

    // check if "ice" value is set
    if(string.IsNullOrEmpty(ice))
        ice = default_ice;    // set "ice" value to the default one

    // check if "sweet" value is set
    if(string.IsNullOrEmpty(sweet))
        sweet = default_sweet;  // set "sweet" value to the default one

    Console.Write(ice);
    Console.Write(sweet);
}
您也可以这样称呼它:

AddSomething(sweet: "1337");
// or
AddSomething("13", "37");
或者随便你怎么想


那么您希望在运行时获取方法参数的默认值,而不必重复您自己的操作,也不必再次键入该值(例如,为了防止出现这种情况,如果您更改了参数的默认值,您也可以在此处更改它)

这并不容易,因为没有
defaultof(参数)
-运算符(类似于
nameof
-运算符)。你必须使用反射

您可以使用此扩展:

public static class MethodExtensions
{
    public static Result<T> ParameterDefault<T>(this MethodBase m, string paramName)
    {
        ParameterInfo parameter = m.GetParameters()
            .FirstOrDefault(p => p.Name == paramName);
        if (parameter == null)
            throw new ArgumentException($"No parameter with given name '{paramName}' found", nameof(paramName));
        if (parameter.ParameterType != typeof(T))
            throw new ArgumentException($"Parametertype is not '{typeof(T)}' but '{parameter.ParameterType}'");

        if(parameter.HasDefaultValue)
            return new Result<T>((T)parameter.DefaultValue, true);
        else
            return new Result<T>(default(T), false);
    }
}
公共静态类方法扩展
{
公共静态结果参数默认值(此MethodBase m,字符串参数名)
{
ParameterInfo参数=m.GetParameters()
.FirstOrDefault(p=>p.Name==paramName);
if(参数==null)
抛出新ArgumentException($“未找到具有给定名称“{paramName}”的参数”,nameof(paramName));
if(parameter.ParameterType!=typeof(T))
抛出新的ArgumentException($”Parametertype不是“{typeof(T)}”,而是“{parameter.Parametertype}”);
if(参数.HasDefaultValue)
返回新结果((T)parameter.DefaultValue,true);
其他的
返回新结果(默认值(T),false);
}
}
它返回以下类的实例,该类只是一个包装器,如果可以确定默认值,它还可以返回信息:

public class Result<T>
{
    public Result(T value, bool success)
    {
        Value = value;
        Success = success;
    }
    public T Value { get; private set; }
    public bool Success { get; private set; }
}
公共类结果
{
公开结果(T值,bool成功)
{
价值=价值;
成功=成功;
}
公共T值{get;私有集;}
公共bool成功{get;private set;}
}
现在,您的方法如下所示:

public void AddSomething(string ice = "10", string sweet = "20")
{
    MethodBase m = MethodBase.GetCurrentMethod();
    if (ice == "")
        ice = m.ParameterDefault<string>(nameof(ice)).Value;
    if (sweet == "")
        sweet = m.ParameterDefault<string>(nameof(sweet)).Value;
    Console.Write(ice);
    Console.Write(sweet);
}
public void AddSomething(string ice=“10”,string sweet=“20”)
{
MethodBase m=MethodBase.GetCurrentMethod();
如果(ice==“”)
ice=m.ParameterDefault(名称(ice)).值;
如果(sweet==“”)
sweet=m.ParameterDefault(nameof(sweet)).Value;
控制台。写入(ice);
控制台。写(甜);
}

您不需要重复参数值。

他希望在运行时访问这些默认值,而无需再次提供它们。与返回参数名称的
nameof
操作符类似,如
nameof(ice)
,他希望
defaultof(ice)
。你必须重复一遍。或者更好:使用string.IsNullOrEmpty()这很有效!!只有我的C#版本不支持“nameof”,谢谢