Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove an enum item from an array

Tags:

c#

.net

enums

In C#, how can I remove items from an enum array?

Here is the enum:

public enum example
{
    Example1,
    Example2,
    Example3,
    Example4
}

Here is my code to get the enum:

var data = Enum.GetValues(typeof(example));

How can I remove Example2 from the data variable? I have tried to use LINQ, however I am not sure that this can be done.

like image 374
user3736648 Avatar asked Jul 20 '15 02:07

user3736648


People also ask

How do you remove an element from an enumeration in Java?

You could also use a static method reference: roles. removeIf(Role. SYSTEM_ADMIN::equals);

Can I iterate through an Enum?

To iterate through an enumeration, you can move it into an array using the GetValues method. You could also iterate through an enumeration using a For... Each statement, using the GetNames or GetValues method to extract the string or numeric value.

Can we iterate Enum in Java?

Iterate over Enum Values: There are many ways to iterate over enum values: Iterate Using forEach() Iterate Using for Loop. Iterate Using java.


1 Answers

You cannot remove it from the array itself, but you can create a new array that does not have the Example2 item:

var data = Enum
    .GetValues(typeof(example))
    .Cast<example>()
    .Where(item => item != example.Example2)
    .ToArray();
like image 130
Sergey Kalinichenko Avatar answered Nov 01 '22 22:11

Sergey Kalinichenko