How to put enum values in a list [SOLVED]
Hi, I'm quite new to C# and I'm still learning, but I'm tasked to debug a piece of code using C#, I managed to fix it but I have like a super long if statement that contains the same thing, except for the values of enum. I came from Python and usually, I'd just declare a list like
my_list = [1, 2]
, but from what I found, I just need to use Enum.GetValues().3 Replies
the if statement looks something like
I need to validate for the second item as well, and my current solution makes it super redundant
So I'm wondering if there's a way for me to put
ItemType.FirstItem
and ItemType.SecondItem
into like an array for the if statement to checkYou can use a ValueTuples like this
Or if you just want to return a value for each condition you can go with pattern matching
I'm actually working with ASP NET as well, my bad I forgot to put in the tag nor mention it. The code after the if statement is taken care of, though I can still refactor it. I'm just looking to perhaps shorten the if statement
Welp, piping it doesn't work, I tried
if (test.IsValid && item.ItemType != (ItemType.FirstItem | Itemtype.SecondItem) && item.File != null)
Ohh I think I found the solution. I just use Enum.GetValues()
and within my if statement, I just wrote if (!test.IsValid && item.ItemType.Equals(listOfItems) && item.File != null)
It works lol
Still, thank you for the help @Kouhai