简体   繁体   中英

Generic type in constructor parameter

I want to use a generic type in a constructor parameter like this:

public class Myclass
{
    public Myclass<T>(IEnumerable<T> ListeValeurChampPerso, string str, int a)
     {
       foreach (var vr in ListeValeurChampPerso)
       {...
       }
    }
}

but it generates me a lot of errors. How can i do this? Thanks in advance.

You have to make the class generic, you can't have a generic constructor because it would be meaningless because you couldn't use T outside of the constructor.

This will work fine.

public class Myclass<T>
{
    public Myclass(IEnumerable<T> ListeValeurChampPerso, string str, int a)
     {
       foreach (var vr in ListeValeurChampPerso)
       {...
       }
    }
}

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