Tom Gullen Tom Gullen - 1 year ago 90
C# Question

Generic enum as method parameter

Given a constructor

public MyObject(int id){
ID = id;

And two enums:

public enum MyEnum1{
Something = 1,
Anotherthing = 2

public enum MyEnum2{
Dodo = 1,
Moustache= 2

Is it possible to pass in a generic enum as a parameter of the constructor? I'm looking for a solution along the lines of:

public MyObject(enum someEnum){
ID = (int)someEnum;

So you can do:

var newObject = new MyObject(MyEnum1.Something);
var anotherObject = new MyObject(MyEnum2.Dodo);

Answer Source

Why do you want to pass the enums, while you could pass integers ?

var newObject = new MyObject((int)MyEnum1.Something);
var anotherObject = new MyObject((int)MyEnum2.Dodo);

and use your first constructor :

public MyObject(int id){
    ID = id;