Aarondv1
Aarondv1

Reputation: 199

Why does the Contains(value) equal false when it contains the value?

Here is the data, a Char-Array (board) and a list of Char-Arrays that are made up different three character sequences from board.

public static char[] board = { 'Q', '-', '+',
                               'A', '-', 'D',
                               '+', 'X', 'C' };

static List<char[]> dalist = new List<char[]> { 
        new char[3] { board[3], board[4], board[5] },
        new char[3] { board[0], board[4], board[8] },
        new char[3] { board[2], board[4], board[6] },
        new char[3] { board[1], board[4], board[7] },

        new char[3] { board[0], board[1], board[2] },
        new char[3] { board[6], board[7], board[8] },
        new char[3] { board[0], board[3], board[6] },
        new char[3] { board[2], board[5], board[8] }
};

static void Main(){   

    foreach (var item in dalist[2])
    {
        Console.WriteLine(item);
    }
    // This returns + - + as expected


    Console.WriteLine(dalist[2].ToString().Contains("-"));
    // This returns false

}

Why does the .Contains method unexpectedly return false?

Upvotes: 1

Views: 103

Answers (7)

Jayaraj.K
Jayaraj.K

Reputation: 928

Modidfy your code from .ToString() to .ToList() ie Change the code as follows

bool isContains = dalist[2].ToList().Contains('-');

Upvotes: 0

gdreamlend
gdreamlend

Reputation: 106

string dalist2 = dalist[2][0].ToString() + dalist[2][1].ToString() + dalist[2][2].ToString();

Console.WriteLine(dalist2.Contains("-"));

or

Console.WriteLine(dalist[2].Contains('-'));

Upvotes: 0

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

Your dataList is not an array of characters, but a list of arrays. So what you´re after is the second element within the third list, or element at [2][1]. So you need this:

Console.WriteLine(datalist[2][1].ToString().Contains("-"));

Which can further be simplified to

Console.WriteLine(datalist[2][1] == '-');

Another approach would be to check if the list contains the character:

Console.WriteLine(datalist[2].Contains('-'));

The reson why your approach doesn´t work is that when calling ToString on an array of characters it won´t simply combine your characters but create something similar to System.Char[] which is obviously not what you expected. The ToString-method isn´t smart enough to first check the elements within and use there string-representation. However it simply returns the type of it´s containing elements.

Upvotes: 0

Brian Rogers
Brian Rogers

Reputation: 129707

daList[2] is a char[]. When you call ToString() on an array, it does not concatenate all the elements of the array into a string, it returns the class name, which in this case is System.Char[]. You can see this for yourself if you do Console.WriteLine(daList[2].ToString()). Since the literal string "System.Char[]" does not contain a hyphen, daList[2].ToString().Contains("-") returns false.

If you want to create a string out of a character array, there is a string constructor for that. I think this is what you really want:

Console.WriteLine(new String(dalist[2]).Contains("-"));

Fiddle: https://dotnetfiddle.net/jLpJLV

Upvotes: 3

Hari Prasad
Hari Prasad

Reputation: 16956

dalist[2] is not a single value, it is char array when you call ToString() on a char array you will get "System.Char[]" not "+-+" as you are thinking..

Console.WriteLine(dalist[2][1].ToString().Contains("-"));
// This will returns true

If you are looking to check for an existence of an item in an array remove ToString() and look for that character.

Console.WriteLine(dalist[2].Contains('-'));

Upvotes: 0

user6522773
user6522773

Reputation:

This one check if "System.Char[]" contains "-" and result is FALSE

Console.WriteLine(dalist[2].ToString().Contains('-'));

This one check if ["+"]["-"]["+"] contains "-" and result is TRUE

Console.WriteLine(dalist[2].Contains('-'));

Upvotes: 0

Mairaj Ahmad
Mairaj Ahmad

Reputation: 14604

You can simply check if datalist2 contains the character - by using Contains method of array

Console.WriteLine(dalist[2].Contains('-'));

Upvotes: 1

Related Questions