How force to use class that implement generic interface in class? Is something like this possible?
public interface IGeneric<T>
{
T Value {get;}
}
//public class MyList<IGeneric<T>> {}//not allowed
CodePudding user response:
Something like this:
void Main()
{
MyList<string> myList = new MyList<string>(new Generic());
}
public interface IGeneric<T>
{
T Value { get; }
}
public class MyList<T>
{
private IGeneric<T> _generic;
public MyList(IGeneric<T> generic)
{
_generic = generic;
}
}
public class Generic : IGeneric<string>
{
public string Value => throw new NotImplementedException();
}
Or like this:
void Main()
{
MyList<Generic, string> myList = new MyList<Generic, string>();
//Or MyList<IGeneric<string>, string> myList = new MyList<IGeneric<string>, string>();
}
public interface IGeneric<T>
{
T Value { get; }
}
public class MyList<G, T> where G : IGeneric<T>
{
}
public class Generic : IGeneric<string>
{
public string Value => throw new NotImplementedException();
}