16

Looking for a way to check if an string contains in another ignoring upper/lower case, I found it:

Works fine. Then, I tried put it to my StringExtensions namespace.

namespace StringExtensions
{

    public static class StringExtensionsClass
    {
        //... 

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

and then:

using StringExtensions;

...

if (".. a".Contains("A", StringComparison.OrdinalIgnoreCase))

but I get the following error:

No overload for method 'Contains' takes '2' arguments

How do I fix it?

Community
  • 1
  • 1
Jack
  • 16,276
  • 55
  • 159
  • 284

2 Answers2

26

When you want to use your extension, add this using statement:

using StringExtensions;

Because of the way Extension methods are declared, visual studio won't find them by itself, and the regular Contains method takes one argument, hence your exception.

Louis Kottmann
  • 16,268
  • 4
  • 64
  • 88
6

I found my mistake:

for this works with dynamic type need use a cast to string. .ToString() method is not sufficient.

if (((string)result.body).Contains(foo, StringComparison.OrdinalIgnoreCase))

Works fine now. Thanks again stackoverflow. :)

Jack
  • 16,276
  • 55
  • 159
  • 284
  • 4
    Right, because it's `dynamic` there's no guarantee that it doesn't have a method called `ToString` that returns something other than a string, so the result of any method call on a `dynamic` is always treated as `dynamic` by the compiler – Davy8 Dec 01 '11 at 18:00