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
CoreVisional
CoreVisionalOP3y ago
the if statement looks something like
if (test.IsValid && item.ItemType != ItemType.FirstItem && item.File != null)
if (test.IsValid && item.ItemType != ItemType.FirstItem && item.File != null)
I need to validate for the second item as well, and my current solution makes it super redundant
if ((test.IsValid && item.ItemType != ItemType.FirstItem && item.File != null) && (test.IsValid && item.ItemType != ItemType.SecondItem&& item.File != null))
if ((test.IsValid && item.ItemType != ItemType.FirstItem && item.File != null) && (test.IsValid && item.ItemType != ItemType.SecondItem&& item.File != null))
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 check
Kouhai
Kouhai3y ago
You can use a ValueTuples like this
void Match(FirstEnum first, SecondEnum second)
{
switch ((first, second))
{
case (FirstEnum.FirstItem, SecondEnum.Invalid):
Console.WriteLine("First and invalid");
break;
case (FirstEnum.SecondItem, SecondEnum.Invalid):
Console.WriteLine("Second and invalid");
break;
default:
Console.WriteLine("Default");
break;
}

}
void Match(FirstEnum first, SecondEnum second)
{
switch ((first, second))
{
case (FirstEnum.FirstItem, SecondEnum.Invalid):
Console.WriteLine("First and invalid");
break;
case (FirstEnum.SecondItem, SecondEnum.Invalid):
Console.WriteLine("Second and invalid");
break;
default:
Console.WriteLine("Default");
break;
}

}
Or if you just want to return a value for each condition you can go with pattern matching
void Match(FirstEnum first, SecondEnum second)
{
var str = (first, second) switch
{
(FirstEnum.FirstItem, SecondEnum.Invalid) => "First and invalid",
(FirstEnum.SecondItem, SecondEnum.Invalid) => "Second and invalid",
_ => null
};

}
void Match(FirstEnum first, SecondEnum second)
{
var str = (first, second) switch
{
(FirstEnum.FirstItem, SecondEnum.Invalid) => "First and invalid",
(FirstEnum.SecondItem, SecondEnum.Invalid) => "Second and invalid",
_ => null
};

}
CoreVisional
CoreVisionalOP3y ago
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
Want results from more Discord servers?
Add your server