I have another problem with my code that I can't fix :\

Basically i have an enum called 'type' in my Item class, which tells me if the item is consumable, or equipment, etc. In the database script I have made, it sets the type for each item (5 in total). So basically I have an inventory UI, with 5 items (inc. 2x consumables, 3x Equip)

After much debugging/checking things, I've found that the boolean prior to "sortedItems.Add(i);" is always FALSE. But it shouldnt be, BOTH VALUES ARE THE SAME as per the console log. I have input a print() line to check whats going on because my items are'nt sorting by type when I expect them to.

public void SortItemsByType(string type)
    foreach (Item i in allItems)
        print("is the following true or false" + i.type.ToString() == type);
        print("this is iToString= " + i.type.ToString());
        print("and this is type: " + type);
        if (i.type.ToString() == type)

and (please note the 3 print lines, here is their output in the console: (sorry but the dumb IDE won't let me copy-paste the console, here is image): console output

Im still very much a noob to this, so I am sure there is something I have missed. But for the life of me i cannot figure out why that bool is always resulting in FALSE?

Any help is massively appreciated. Thanks


2 Answers 2


Quick Answer: What your code actually currently does is compare references to check that they are the same, not the values of those references.

Try changing

print("is the following true or false" + i.type.ToString() == type);


print("is the following true or false" + string.Equals(i.type.ToString (), type));

When you use string.Equals, you're comparing the values instead of the references themselves. '==' checks to see that the references.

See more info below

https://stackoverflow.com/questions/814878/c-sharp-difference-between-and-equals https://www.dotnetperls.com/string-equals https://msdn.microsoft.com/en-us/library/system.string.equals(v=vs.110).aspx

  • \$\begingroup\$ thanks very much im looking at this now to try fix and will report back asap. Ta :) \$\endgroup\$ Commented Jul 16, 2017 at 22:23
  • \$\begingroup\$ Thanks that a step forward! Now the boolean is answering true and false when i expect it to, but wierdly I seem to have broken something as the items still arent changing after I get the SortedItems.Add(i) to call. Hmmm i will keep looking into it, as its pretty much impossible to type up the issue. Thanks for steering me past that particular sticky spot \$\endgroup\$ Commented Jul 16, 2017 at 22:40
  • \$\begingroup\$ Are you using '==' or string.Equals() for the final check before you add items to your sorted list? Remember the former won't work properly to compare the values of the strings, just like that print statement issue. \$\endgroup\$ Commented Jul 17, 2017 at 19:38

Your first logging line isn't doing what you think it's doing. Unlike Java, C# overloads operator == on strings so as long as type is a string you don't need the .Equals.

print("is the following true or false" + foo == bar); // wrong 
print("is the following true or false" + (foo == bar)); // right

Otherwise, the function looks like it should work (at least, as far as updating the sortedItems list)

  • \$\begingroup\$ Thanks for your answers guys. I have moved on without fully fixing the problem im afraid. But using the string.Equals() enabled me to get the expected output from the bool \$\endgroup\$ Commented Jul 18, 2017 at 22:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .