繁体   English   中英

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

[英]C#: How to set default value if input is empty

如果我有这样的方法:

public void AddSomething(string ice = "10", string sweet = "20")
{
    Console.Write(ice);
    Console.Write(sweet);
}

所以,如果我输入一个字符串,它将写入字符串。 如果没有,它将写入默认字符串(10,20)

但是我想要这样的东西:

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 ice == "" || ice == null

所以你想在运行时获取方法参数的默认值而不重复自己,所以不要再输入那个值(如果你改变参数的默认值那么你也可以改变它吗?)

这并不容易,因为没有defaultof(parameter) (类似于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);
    }
}

返回以下类的实例,它只是一个包装器,如果可以确定默认值,则还可以返回信息:

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; }
}

现在你的方法看起来像:

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);
}

而且您不需要重复参数值。

这个问题(至少对我而言)尚不清楚,但从你发布的内容我可以建议这样的解决方案:

// 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");

或者你喜欢。

在线试试吧

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM