Link to home
Start Free TrialLog in
Avatar of dh-s
dh-s

asked on

C# If Item is not in array

Hello,

What I want to be able to do, in C#, is not do something if the item is in an array.

I have a for loop going through a list of items, and it is modifying them. There are some of them that I don't want modified, and I have stored the text of them in a pre-defined array. What I want, is the for loop to go through the array of elements that I want modified, but if the text matches any of the items in another array that I have defined, to not do anything.

Is there any easy way to do this?

Thanks.
Avatar of josgood
josgood
Flag of United States of America image

If you make a generic List of don't modify strings as in
   List<string> dontModify= new List<string>();

and then add each text string that shouldn't be modified to the List
      dontModify.Add("nochange1");
      dontModify.Add("nochange2");

then you can
   struct Item {
      public string someText;
   };
   Item myItem = new Item(); // you'll have to initialize it correctly, of course
   if (dontModify.Contains(myItem.someText)) Console.WriteLine("Don't change");

ASKER CERTIFIED SOLUTION
Avatar of shman911
shman911

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial