Sturm
Sturm

Reputation: 4125

Not equal string

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

Answers (4)

Maloric
Maloric

Reputation: 5605

It should be this:

if (myString != "-1")
{
    //Do things
}

Your equals and exclamation are the wrong way round.

Upvotes: 18

Vivek Nuna
Vivek Nuna

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

Gerold Broser
Gerold Broser

Reputation: 14762

With Equals() you can also use a Yoda condition:

if ( ! "-1".Equals(myString) )

Upvotes: 1

Odys
Odys

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

Related Questions