Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# passing generic type to another class [duplicate]

I wanna know is there any way to pass generic type to another Class as an argument. In other words. I have SomeClass<T> and AnotherClass. I wanna AnotherClass to have an instance field of Type <T> who would be initialized in constructor.

(I want SomeClass to be list of AnotherClass objects. Another Class would have 3 instance fields reference to previous AnotherClass object reference to next AnotherClass object and a T type field.

like image 321
user2184057 Avatar asked Nov 04 '22 02:11

user2184057


1 Answers

Sounds like you're making a generic container. You need something like:

class Container<T>
{
    public T Value;

    public Container( T rhs )
    {
        Value = rhs;
    }
}

That's basic generics in C#. If you provide more description, I can better answer your question, but based on the info provided, this is what you're looking for.

like image 152
Will Custode Avatar answered Nov 12 '22 21:11

Will Custode