in Technology by
How to loop through all enum values in C#?

1 Answer

0 votes
by

Yes you can use the ‍GetValue‍‍‍s method:

var values = Enum.GetValues(typeof(Foos));

Or the typed version:

var values = Enum.GetValues(typeof(Foos)).Cast<Foos>();

I long ago added a helper function to my private library for just such an occasion:

public static class EnumUtil {
    public static IEnumerable<T> GetValues<T>() {
        return Enum.GetValues(typeof(T)).Cast<T>();
    }
}

Usage:

var values = EnumUtil.GetValues<Foos>();

Related questions

0 votes
    How can an int be cast to an enum in C#?...
asked Jan 15, 2021 in Technology by JackTerrance
0 votes
    How can we enumerate an enum in C#?...
asked Jan 15, 2021 in Technology by JackTerrance
0 votes
    I have a number of user permissions that are tested throughout my ASP.NET application. These permission ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    What does the Enum Attribute mean in C#?...
asked Jan 16, 2021 in Technology by JackTerrance
0 votes
    Which is the function used to loop in an array to view all the values? (a) all() (b) loop( ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 21, 2021 in Education by JackTerrance
0 votes
    Which is the function used to loop in an array to view all the values? (a) all() (b) loop() (c) ... Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 21, 2021 in Education by JackTerrance
+1 vote
    ____ allows you to loop through a block of code as long as the specified condition is true. a)Tag b)While c)For...
asked Oct 8, 2020 in Technology by JackTerrance
0 votes
    Can anyone tell me how I can represent the equivalent of an Enum in Python? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I have 10 worksheets. I want to create a table for each. every table has a different amount of ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I have 10 worksheets. I want to create a table for each. every table has a different amount of ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    Let's say I have the following simple table variable: declare @databases table ( DatabaseID int, Name ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    What I'd like to do is iterate through a navigation's list items, and fade each element in, ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 17, 2022 in Education by JackTerrance
0 votes
    I have a dataframe with 2 columns and I want to add the new column; This new column should be updated based on ... +=1 is not working. Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    16. To loop through a set of code a fixed number of times, ……..function is used. * 1 point print() input() range() Select the correct answer from above options...
asked Dec 25, 2021 in Education by JackTerrance
0 votes
    Which activity can you use if you want to loop through a collection of items in uipath? Select the correct answer from above options...
asked Dec 1, 2021 in Education by JackTerrance
...