Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum extension method to get all values

Tags:

c#

I want to do something like this:

enum MyEnum { None, One, Two };

var myEnumValues = MyEnum.Values();

My extension method:

        public static IEnumerable<T> Values<T>(this Enum enumeration)
             where T : struct
         => Enum.GetValues(typeof(T)).Cast<T>();

But it looks this way:

MyEnum.None.Values<MyEnum>(); 

How to do it?

like image 424
Nerf Avatar asked Jan 27 '26 02:01

Nerf


1 Answers

Extension methods are static methods that are applied to an instance of an object.

MyEnum is a type, and not an instance, so you can't add extension methods to it.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!