I am getting an error when trying the following in C#
if (state != 'WI' && state != 'IL')
That statement is giving me an error that states: Error operator != cannot be applied to operands of type 'string' or 'char'
If this开发者_Go百科 is not possible what is a way to accomplish my goal.
Use double quotes for strings:
if (state != "WI" && state != "IL")
Single quotes are useful for single characters:
char c = 'A';
if (c != 'B') ...
EDIT: others have suggested using Equals
to compare and I don't fully agree that it should replace an ==
approach unless you have a reason to use it. Firstly, if state
is null
then an exception will be thrown from writing state.Equals("WI")
. A way around this is to use String.Compare(state, "WI")
instead but that no longer returns a bool
and will need to be checked against an integer (0 is returned if they are the same):
if (String.Compare(state, "WI") != 0)
Secondly, I would suggest using either Equals
or String.Compare
if case-sensitivity matters since both provide overloads to handle that issue:
string foo = "Foo";
string otherFoo = "foo";
Console.WriteLine("Equals: {0}", foo.Equals(otherFoo));
Console.WriteLine("Equals case insensitive: {0}", foo.Equals(otherFoo, StringComparison.InvariantCultureIgnoreCase));
Console.WriteLine("Compare: {0}", String.Compare(foo, otherFoo) == 0);
Console.WriteLine("Compare case insensitive: {0}", String.Compare(foo, otherFoo, StringComparison.InvariantCultureIgnoreCase) == 0);
// make foo null
foo = null;
Console.WriteLine("Null Compare: {0}", String.Compare(foo, otherFoo) == 0);
Console.WriteLine("Null Equals: {0}", foo.Equals(otherFoo)); // exception
Use double quotes to enclose string literals!
You are using single quotes to delimit your strings. These mean char
.
Try:
if (state != "WI" && state != "IL")
with double quotes.
While the code you have is perfectly OK, you should consider using the string.Equals
method:
if (!state.Equals("WI") && !state.Equals("IL"))
You should use "WI" also use equals method of string as !state.Equals("WI")
if ( !state.Equals("WI") && !state.Equals("IL") ) {
}
If this is not possible what is a way to accomplish my goal.
RTFEM!* The error message is clearly indicating that one of the operands is being interpreted as a char
and not a string
. In the case of
state != 'WI'
there are two operands: state
and 'WI'
. If you've declared state
as a string
, then clearly it can't be state
that is being interpreted as a char
. Thus, it must be 'WI'
that is being interpreted as a char
. And now you ask yourself, "Why is this being interpreted as a char
? How do I declare a string
in C# if not enclosing in single-quotes ("'
")?" So then you Google "declare string C#" and click "I'm Feeling Lucky" and you hit Strings (C# Programming Guide) which tells you to declare a string
literal you enclose in double-quotes (""
").
Thus,
if (state != "WI" && state != "IL")
* This is not meant to be rude so don't take it that way. This answer is very much in the philosophy of "If you teach a man to fish...."
精彩评论