12

Can someone explain to me why the top part of the code works but when test is an array it doesn't?

string test = "Customer - ";
if (test.Contains("Customer"))
{
    test = "a";
}

The code below doesn't work

string[] test = { "Customer - " };
if (test.Contains("Customer"))
{
    test[0] = "a";
}
AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52
Laura Bejjani
  • 151
  • 1
  • 1
  • 10
  • 1
    The two types - `string` & `string[]` - are different, so the `.Contains` method for each is a different method, even though it has the same name, and each does a different thing. – Enigmativity Jul 05 '17 at 04:33

4 Answers4

14

In first case, you call String.Contains which checks if string contains substring.
So, this condition returns true.

In the second case, you call Enumerable.Contains on string[] which checks if string array contains a specific value.
Since there is no "Customer" string in your collection, it returns false.

These are two similarly called but actually different methods of different types.

If you want to check if any string in collection contains "Customer" as a substring, then you can use LINQ .Any():

if (test.Any(s => s.Contains("Customer"))
{
    test[1] = "a";
}
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
3

First snippet searches for string Customer inside another string Customer - which works but in second snippet string Customer is searched inside array of strings one of which is Customer -. Hence the second returns false.

This is evident from how you declare test

string test = "Customer - ";

OR

string[] test = { "Customer - " };

Notice the curly brackets. That's how you define a collection (array here)

To make second snippet work you should do

string[] test = { "Customer - " };
if (test.Any(x => x.Contains("Customer")))
{
    test[1] = "a";
}
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
  • Thanks I think the second snippet is a little misleading because it's checking for an exact match and not a real contains. My test array has more values than what I stated here. How do I check the index for that if? – Laura Bejjani Jul 06 '17 at 02:02
  • You need to run a plain old for loop from `0` to `array.length-1`. when the value is found, the loop value is the index. Other than that you can do it with linq. https://stackoverflow.com/a/10443540/1011959 – Nikhil Agrawal Jul 06 '17 at 02:32
1

Because in second case your if condition is false as you are searching for first string in array of strings. Either you can use any keyword to search for any string in array that match customer or you can search test[0] to search first string in array if it matches to customer like:

if (test[0].Contains("Customer"))
    {
        //your rest of the code here
    }


 if (test.Any(x => x.Contains("Customer")))
    {
        //your rest of the code here
    }
Preet
  • 984
  • 2
  • 14
  • 34
  • You don't understand the context of the question, so, ultimately your given answer is incorrect. Either remove or edit your answer. – M. Adeel Khalid Jul 05 '17 at 08:06
1
string test = "Customer - ";
if (test.Contains("Customer"))
{
  test = "a";
}

In this Contains is a String Class Method it's base of IEnumerable checks

"Customer -" having Customer.

But

string[]test = { "Customer - " };
if (test.Contains("Customer"))
{
   test[1]= "a";
 }

In this Place Contains is a IEnumerable Method it's base of IEnumerable checks.So, It's Not Working You Change the Code

string[] test = { "Customer - " };
if (test.Any(x => x.Contains("Customer")))
{

}

test[1]="a" It's throw Exception because, the array position start with 0 not 1

umasankar
  • 599
  • 1
  • 9
  • 28