简体   繁体   English

如果它是 null 或为空,如何将默认值设置为字符串

[英]How to set default values to string if it is null or empty

Is there Better way to check if a string is empty or null using Null Coalescing operator.When I use empty value to a string instead of null the null Coalescing operator failed to get me the desired result. Is there Better way to check if a string is empty or null using Null Coalescing operator.When I use empty value to a string instead of null the null Coalescing operator failed to get me the desired result.

 string x = null;
 string y = x ?? "Default Value";
 Console.WriteLine(y);
    

Here if I replace x = null with x="" this doesnt work.在这里,如果我将x = null替换为x=""这不起作用。

If I use String.IsNullOrEmpty method如果我使用String.IsNullOrEmpty方法

if(String.IsNullOrEmpty(x)
{
    y= "default value"
}
{
    y =x 
}
    

My code block is having multiple lines and I want to make it simple.我的代码块有多行,我想让它变得简单。 Can you suggest a better way to keep the code clean and simple.您能否提出一种更好的方法来保持代码简洁明了。 As it is used in many places.因为它在许多地方使用。

You can use the ?你可以使用? operator操作员

string x = null;
string y = string.IsNullOrEmpty(x) ? "Default Value" : x;
Console.WriteLine(y);

Given that you say "it is used in many places" then it might be appropriate to write an extension method to help with this:鉴于您说“它在很多地方都使用过”,那么编写一个扩展方法来帮助解决这个问题可能是合适的:

public static class StringExt
{
    public static string OrIfEmpty(this string? self, string defaultValue)
    {
        return !string.IsNullOrEmpty(self) ? self : defaultValue;
    }
}

Which you would use like so:你会这样使用:

string? x = null;

string y = x.OrIfEmpty("test");

Console.WriteLine(y); // "test"

You could probably choose a better name for OrIfEmpty() depending on taste.您可能会根据口味为OrIfEmpty()选择更好的名称。

However, note that this suffers from the drawback that the default value is always evaluated.但是,请注意,这样做的缺点是始终评估默认值。 If obtaining the default is expensive, then using this extension method will hurt performance because the default will always be evaluated even if it's not used.如果获取默认值代价高昂,那么使用此扩展方法将损害性能,因为即使未使用默认值,也会始终对其进行评估。

To circumvent that issue you'd add an extension method to allow you to pass a Func<string> instead:为了规避这个问题,你需要添加一个扩展方法来允许你传递一个Func<string>

public static string OrIfEmpty(this string? self, Func<string> defaultValue)
{
    return !string.IsNullOrEmpty(self) ? self : defaultValue();
}

Then when calling it you'd have to pass a delegate for the default, eg:然后在调用它时,您必须为默认值传递一个委托,例如:

public sealed class Program
{
    public static void Main()
    {
        string? x = null;

        string y = x.OrIfEmpty(expensiveDefault);
        
        Console.WriteLine(y);
    }

    static string expensiveDefault()
    {
        return "test";
    }
}

I'm just posting this for consideration.我只是发布这个以供考虑。 Myself, I would just use Marco's answer.我自己,我只会使用 Marco 的答案。 But if you have a LOT of code that does it, then just maybe...但是如果你有很多代码可以做到这一点,那么也许......

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

相关问题 如何检查“id”是空还是空,那么默认值是0还是空字符串? - How to check if the “id” is null or empty then default values are 0 or empty string? 传递空值时如何设置空字符串 - How to set empty string when null values are passed 如何将DataTable中的空数据设​​置为空字符串 - How to set null data in DataTable to empty string UiPath:如何将默认值设置为 String [] 变量 - UiPath: How to set default values to a String[] variable System.Text.Json:反序列化时,如何忽略在构造函数中设置了默认值的属性的 null 或空字符串值? - System.Text.Json: when deserializing, how can I ignore a null or empty string value for a property that has a default value set in the constructor? 在C#中默认将值设置为EMPTY但不是NULL - Setting values to EMPTY but not NULL by default in C# MVC保存空字符串默认值null - MVC save empty string default value null 如果为null,则将其设置为linq中的空字符串lambda表达式 - If null then set it to empty string lambda expression in linq 区分XElement中的null和string.Empty值 - Differentiate null and string.Empty values in an XElement 为什么字符串类型的默认值是 null 而不是空字符串? - Why is the default value of the string type null instead of an empty string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM