Skip to content

Instantly share code, notes, and snippets.

@sachintha81
Last active December 6, 2019 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sachintha81/e7d52a133fe3ae97e9e1e260c740885a to your computer and use it in GitHub Desktop.
Save sachintha81/e7d52a133fe3ae97e9e1e260c740885a to your computer and use it in GitHub Desktop.
Loop through Enum
// Typed version
var values = Enum.GetValues(typeof(Foos)).Cast<Foos>();
// Helper function
public static class EnumUtil {
public static IEnumerable<T> GetValues<T>() {
return Enum.GetValues(typeof(T)).Cast<T>();
}
}
// Usage:
var values = EnumUtil.GetValues<Foos>();
// For-each
foreach(Foos foo in Enum.GetValues(typeof(Foos)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment