Search code examples
c#asp.net

if not equal condition in c#


I have a condition where the value should not be null or it should be either R or U.

I had tried with the below method, but I'm still not able to succeed with the result.

if (Place == "" || Place != "R" || Place != "U")
{
    response.Status = "FAILURE";
    response.Message = "Place Criteria is required / Invalid (place should be either R(rural)/U(urban)";
}

even though I pass R or U it shows the error message.


Solution

  • if(Place ==""||( Place!="R" && Place !="U"))
    {
    
    }
    

    you want the Place not be null and be "R" or "U" so you should use && to make sure the 3(null,"R","U") do not happen at the same time.