Home c# C # templates and lists

C # templates and lists

Author

Date

Category

There is a class colum & lt; T & GT; , there is a class Row with a list LIST & LT; Colum & GT; . And there is a problem, list & lt; colum & gt; requires another type t Aki List & lt; colum & lt; int & gt; & gt; , but I do not need it, I need to ROW kept a list of Colum and did not think about what type of colum template. What can you advise? (I know about Dynamic, but trying to do without it)


Answer 1, Authority 100%

Make a regular class column , inhibit generalized classes (column & lt; T & GT;: column ), in Row Keep the basic list List & lt; Column & GT; .

just as an example

Public Abstract Class Column
{
  Public Abstract Object ValueObject {Get; }
}
Public Class Column & Lt; T & GT; : Column.
{
  Private T TypedValue;
  Public Column (T Value)
  {
    typedvalue = value;
  }
  Public Override Object ValueObject = & GT; typedvalue;
  Public T Value = & gt; typedvalue;
}

Row

Public Class Row
{
  Private List & LT; Column & GT; _List = New List & lt; column & gt; ();
  Public Void Add (Column C) = & gt;
    _List.add (C);
  Public Void Print () = & gt;
    _List.Foreach (V = & GT; Console.Writeline (v.ValueObject));
}

How to use

var row = new row ();
row.add (new column & lt; int & gt; (5));
row.add (new column & lt; string & gt; ("vasya"));
row.print ();

output

5
Vasya.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions