繁体   English   中英

C# 3.5 部分 class 字符串 IsNullOrWhiteSpace

[英]C# 3.5 partial class String IsNullOrWhiteSpace

我正在尝试为String class ( IsNullOrWhitespace ,如 .NET4 )创建额外的功能,但我在引用时遇到问题:

错误 1 'String' 是 'string' 和 'geolis_export.Classes.String' 之间的模糊引用

我不想创建扩展方法。 因为如果string x = null;这将崩溃

用法:

private void tbCabineNum_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    e.Handled = !e.Text.All(Char.IsNumber) || String.IsNullOrWhiteSpace(e.Text);
}

字符串部分:

public partial class String
{
    public static bool IsNullOrWhiteSpace(string value)
    {
        if (value == null) return true;
        return string.IsNullOrEmpty(value.Trim());
    }
}

不能为String class 创建额外内容吗? 我试图将部分放在System命名空间中,但这会产生其他错误。

String重命名为String2也可以解决问题。 但这不是我想要的,因为那时没有引用原始String class。

不可能这样,因为.NET框架中的string class不偏。
相反,使用像这样的真正的扩展方法:

public static class StringExtensions
{
    public static bool IsNullOrWhiteSpace(this string value)
    {
        if (value == null) return true;
        return string.IsNullOrEmpty(value.Trim());
    }
}

用法将是这样的:

string s = "test";
if(s.IsNullOrWhiteSpace())
    // s is null or whitespace

与所有扩展方法一样,如果字符串为null ,则调用不会导致 null 引用异常:

string s = null;
if(s.IsNullOrWhiteSpace()) // no exception here
    // s is null or whitespace

出现这种行为的原因是编译器会将这段代码翻译成等效于以下 IL 代码的 IL 代码:

string s = null;
if(StringExtensions.IsNullOrWhiteSpace(s))
    // s is null or whitespace

扩展方法必须定义为 static class 中的static 方法 还要注意参数上的this关键字。

public static class MyExtensions
{
    public static bool IsNullorWhitespace(this string input)
    {
         // perform logic
    }
}

您通过在static上省略 static 所做的是在您的程序集中定义一个竞争的 class ,因此来自编译器的模棱两可的消息。

string class 未声明为部分字符串,您必须改为编写扩展方法。

public static class MyExtensions
{
    public static bool IsNullOrWhiteSpace(this string value)
    {
        if (value == null) return false;
        return string.IsNullOrEmpty(value.Trim());
    }
}

这不是您创建扩展方法的方式。 class 不是部分的,它需要是 static class 并且可以命名为任何名称(MyExtensionMethods)。 您还需要在扩展方法上用“this”标记您的参数。

试试这个

public static class StringExtensions
{
    public static bool IsNullOrWhiteSpace(this string value)
    {
        if (value == null) return true;
        return string.IsNullOrEmpty(value.Trim());
    }
}

为了创建扩展方法,您需要使用以下语法。 (注意关键字this的使用):

public static bool IsNullOrWhiteSpace(this string value)

修剪字符串会导致可避免的字符串分配(这会损害性能)。 最好依次检查每个字符而不分配任何内容:

        public static bool IsNullOrWhiteSpace(this string value)
        {
#if NET35
            if (value == null)
                return true;

            foreach (var c in value)
            {
                if (!char.IsWhiteSpace(c))
                {
                    return false;
                }
            }

            return true;
#else
            return string.IsNullOrWhiteSpace(value);
#endif
        }

#if NET35代码意味着回退实现仅在针对 .NET 3.5 时存在。 您可以根据需要调整该比较以满足项目的目标框架。 否则,将使用默认的string.IsNullOrWhiteSpace方法,并且此 util 方法可能会被内联。

暂无
暂无

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

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