generic constrains for enum #69730
-
I think the generic constrain should not accept the Yoo format. Since you can define it, but can not use it as type. Could someone help to point out how to use this Yoo generic since I can define it. public class Foo<T> where T : struct, Enum
{
public T Value { get; set; }
}
public class Yoo<T> where T : class, Enum
{
public T Value { get; set; }
}
public class Bar
{
public Foo<Goo> Goo { get; set; } = new();
// this line will cause compiling error!
public Yoo<Goo> Yoo { get; set; } = new();
}
public enum Goo
{
A,
B,
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
you can do |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi thanks for your reply. But I can`t define Foo<System.Enum> as type now. Maybe the Foo should not have the constrains both for struct and Enum at the same time. |
Beta Was this translation helpful? Give feedback.
-
I found if I remove Enum from Foo constrains, then |
Beta Was this translation helpful? Give feedback.
Correct. But you asked
how to use this Yoo generic
. You can use theYoo
generic with theSystem.Enum
type. You can use theFoo
generic with any actualenum
type.The
Foo
generic makes complete sense. It is saying that to be instantiated the type must be a subtype of System.Enum, and that it must be a struct. In .net today there are tons of types that satisfy this. Namely, every singleenum
.The
Yoo
generic is odd, but still makes sense. It is saying that to be instantiated the type must be a subtype of System.ENum (or System.En…