IsNullOrEmpty'在当前上下文中不存在

时间:2018-09-02 09:17:32

标签: c#

我有此代码:

public static FormattedString AddParagraph(this FormattedString formattedString, string text)
    {
        if (IsNullOrEmpty(text))
            return formattedString;
        else
        {
            formattedString.Spans.Add(new Span { Text = text + Environment.NewLine + Environment.NewLine, ForegroundColor = Color.FromHex("555555") });
            return formattedString;
        }
    }

但是它告诉我:名称“ IsNullOrEmpty”在当前上下文中不存在

任何人都可以给我建议,如何检查此字符串是否为“”或是否包含“”以外的内容?

2 个答案:

答案 0 :(得分:3)

应为 string.IsNullOrEmpty(text)

编辑 您需要使用上面的字符串,因为string.IsNullEmpty()是一个静态方法,可以在您的静态方法中使用它。

答案 1 :(得分:3)

如果您想独立使用IsNullOrEmpty,则可以使用C#6的using static功能。您只需要在代码中添加相关的using

using static System.String;