Collection / string. Consoles vs collection / string.IndexOf

Is there a reason to use. Contains a string / list instead of .IndexOf? Most of the codes that I would write using .Contains shortly afterwards will require an element index, and therefore both statements will have to be made. But why not both in one?

if ((index = blah.IndexOf(something) >= 0)
    // i know that Contains is true and i also have the index
+5
source share
3 answers

You are right, which IndexOfis a more general operation than Contains. However Contains, it is still very useful because it clearly represents the operation.

if(blah.IndexOf(something) >=0)
{
}

not as obvious an operation as

if(blah.Contains(something))
{
}

, , IndexOf, , , Contains.

, .

+7

, Contains, , , IEqualityComparer<String> ( , , - ). IndexOf .

+2

, , IndexOf Contains.

? Linq, sql, SQL-, IndexOf

0

All Articles