简体   繁体   中英

How can I access enum in Class<T> in C#?

I got an error CS0305 with this piece of code. Is there a good way to do this?

public class Filtre<T> : List<KeyValuePair<Filtre.Methode, object>>
{
    public enum Methode
    {
        Id,
        Keyword
    }
}  

If you really want to keep the enum inside Filtre<T> , then change the class signature as -

public class Filtre<T> : List<KeyValuePair<Filtre<T>.Methode, object>>
{
    public enum Methode
    {
        Id,
        Keyword
    }
}

But does that make any sense? Think about it, does the value of T by any means affect the enum Methode ? No. Rather its adding unnecessary friction in code.

Also, you cannot declare the enum as anything less accessible than public , because then you will be making the base class List<KeyValuePair<Filtre<T>.Methode, object>> less accessible than Filtre<T> .

So, it will be better to declare the enum outside, and simplify the class signature -

public enum Methode
{
    Id,
    Keyword
}

public class Filtre<T> : List<KeyValuePair<Methode, object>>
{
    //
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM