Like my stuff? I'm now live coding on Twitch several times a week. Come join me while I build software, teach concepts, and sometimes play 8 bit video games.

Jeremy Morgan on Twitch

I just had this problem tonight, so I thought I’d share the solution. In many languages you can do a string comparison in the following way:

if ($city == "Portland" || $city == "Seattle") {
// stuff goes here

And this works just fine. But in C#, strings are treated as objects, so you have to do the same comparison as such:

if ((city == "Portland") || (city == "Seattle")) {
// stuff goes here

In the above example, if the city is Portland, it will evaluate as true, making it a boolean. This is a simple step and an easy fix, but I couldn’t readily find anything on google about it, so I thought I’d post it here.

If you want to use the || or operator in C#, this is how you do it.

Get notified when new tutorials are posted:

Published: Dec 8, 2008 by Jeremy Morgan. Contact me before republishing this content.