Reputation: 4125
I'm trying to set a condition like this
if (myString=!"-1")
{
//Do things
}
But it fails. I've tried
if(myString.Distinct("-1"))
{
//Do things
}
but it doesn't work either.
Upvotes: 7
Views: 135256
Reputation: 5605
It should be this:
if (myString != "-1")
{
//Do things
}
Your equals and exclamation are the wrong way round.
Upvotes: 18
Reputation: 1
Now in C# 9, it can be done in a more readable way like this.
string str = "x";
if(str is not "y")
{
...
}
Upvotes: 0
Reputation: 14762
With Equals()
you can also use a Yoda condition:
if ( ! "-1".Equals(myString) )
Upvotes: 1
Reputation: 9080
Try this:
if(myString != "-1")
The opperand is !=
and not =!
You can also use Equals
if(!myString.Equals("-1"))
Note the !
before myString
Upvotes: 21