简体   繁体   中英

How to create a refence a generic class in a non generic class

is there a way of referencing a generic class in a non generic class eg

public class IGenericClass<T> where T: class
{
}

public class GenericClass<T> : IGenericClass<T> where T : class
{   }

how best do i create a refence for the IGenericClass in a concrete class.

thanks for your assistance

Yes, you can. However, it should be a closed constructed type, meaning T should be known upfront.

class Client
{
    private IGenericClass<T> instance;// Illegal[1]
    private IGenericClass<object> instance;// Legal, It can be any type.
}

Also, don't name classes with I Prefix which may give the impression that it is an interface when it is not.

1. It is legal if you have a type named T , otherwise not

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