Home > front end >  Cannot use != operator for a list of values
Cannot use != operator for a list of values

Time:10-07

Im trying to create a list of uint values and then use that variable/list in my code to make sure that none of those values inside the list are being used by my code.

Trying to use this method i get an error that "Operator '!=' cannot be applied to operands of type 'uint' and 'List<uint>"

var list = new List<uint> { 7, 2, 1 };
if(Execute() && InAction() && ID != list 
  return Action;

And other ideas would be appreciated

CodePudding user response:

A value and a list are neither equal nor not equal*; instead, use Contains to check whether the value exists in the list

if (Execute() && InAction() && !list.Contains(ID))

* except for pedantic scenarios for example where a List<object> has had itself added to itself; but... yeah

CodePudding user response:

Do this:

var list = new List<uint> { 7, 2, 1 };
if(Execute() && InAction() && !list.Contains(ID)) 
{
   return Action;
}
  • Related