简体   繁体   English

如何覆盖Contains()?

[英]How to override Contains()?

The Contains(...) method for string is case-sensitive. String的Contains(...)方法区分大小写。 I'd like to override it in order to make it case-INsensitive with the following code (which is stolen from here ): 我想覆盖它,以便使用以下代码(从这里被盗)使其为case-INsensitive:

public static bool Contains(this string source, string toCheck, StringComparison comp)
{
    return source.IndexOf(toCheck, comp) >= 0;
}

However, I don't know where the code should be pasted. 但是,我不知道应该在哪里粘贴代码。 Should it be placed inside the same namespace of the class program? 它应该放在类程序的同一名称空间中吗? Does it need a dedicated class? 它需要一个专门的课程吗?

If what you are intending is to create an extension method for the string class, then you need to put it inside some class. 如果您打算为string类创建扩展方法 ,那么您需要将其放在某个类中。 To use it, simply make sure you have a using statement specifying a reference to the namespace containing the class. 要使用它,只需确保您有一个using语句,指定对包含该类的命名空间的引用。

For example: 例如:

namespace SomeNamespace
{
    public static class StringExtensions
    {
        public static bool Contains(this string source, string toCheck, StringComparison comp)
        {
            return source.IndexOf(toCheck, comp) >= 0;
        }
    }
}

// ... In some other class ...
using SomeNamespace;

// ...
bool contains = "hello".Contains("ll", StringComparison.OrdinalIgnoreCase);

As you are creating an extension method, you can put it into any namespace and any static class: 在创建扩展方法时,可以将其放入任何命名空间和任何静态类:

namespace MyProject.MyNamespace
{
    public static class MyExtensions
    {
        public static bool Contains(this string source, string toCheck, StringComparison comp)
        {
            return source.IndexOf(toCheck, comp) >= 0;
        }
    }
}

This is purely an organisational thing. 这纯粹是一种组织性的东西。 You will need to add the necessary using MyProject.MyNamespace; 您需要using MyProject.MyNamespace;添加必要的内容using MyProject.MyNamespace; statements where ever you use the code of course. 你当然使用代码的陈述。

You'll need to put this extension method somewhere where it makes sense for you, for instance a utility class, which has to be static. 您需要将此扩展方法放在适合您的地方,例如实用程序类,它必须是静态的。 You can't actually override anything from string, that's why you're using the extension method. 你实际上无法覆盖字符串中的任何内容,这就是你使用扩展方法的原因。

Example: 例:

public static class StringExtension
{
    public static bool Contains(this string source, string toCheck, StringComparison comp)
    {
        return source.IndexOf(toCheck, comp) >= 0;
    }
}

I would suggest putting it in some sort of utility class that you make, which contains nice helper methods such as this. 我建议把它放在你做的某种实用类中,它包含很好的帮助方法,比如这个。 If you added it to, say, a StringUtils class you could just call StringUtils.Contains() to use it. 如果你把它添加到StringUtils类中,你可以调用StringUtils.Contains()来使用它。

This is an Extension Method . 这是一种扩展方法 You could just create a class called MyStringExtension. 您可以创建一个名为MyStringExtension的类。 But the name is not really relevant. 但这个名字并不真正相关。 As long as the code file has the namespace defined, where your MyStringExtension class is defined, all your strings will have that method. 只要代码文件定义了命名空间,定义了MyStringExtension类,所有字符串都将具有该方法。

This method is called Extension method 此方法称为Extension method

You can have it in your custom namespace within static class and use it by using your custom namespace. 您可以在静态类中的自定义命名空间中使用它,并使用自定义命名空间来使用它。

Instead overriding (which you can't do) or creating an extension method, I suggest you use the following Contains overload and write custom IEqualityComparer instead. 而是覆盖(您不能做)或创建扩展方法,我建议您使用以下Contains重载并编写自定义IEqualityComparer

public static bool Contains<TSource>(
    this IEnumerable<TSource> source, 
    TSource value, 
    IEqualityComparer<TSource> comparer);

That is an extension method , so it will be available on any string object that has access to your extension method. 这是一种扩展方法 ,因此它可以在任何有权访问扩展方法的字符串对象上使用。

You might define it like this 您可以像这样定义它

namespace Example.ExtensionMethods
{
    public static class StringExtensions
    {
        public static bool Contains(this string source, string toCheck, StringComparison comp)
        {
            return source.IndexOf(toCheck, comp) >= 0;
        }
    }   
}

And use like this 并使用这样的

using Example.ExtensionMethods;

string s = "foobar";
int i = s.Contains("oob", StringComparison.InvariantCultureIgnoreCase);

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

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