Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does T represent in this class signature and how can i Instantiate this class?

Tags:

c#

I am looking at some of my professor's code for him while he is on Holiday and i came across this unreferenced class. Can someone please explain what T represents and possibly how to instantiate a class such as this?

public class RowToObject<T> where T : new()
like image 230
some_bloody_fool Avatar asked Dec 12 '22 07:12

some_bloody_fool


2 Answers

T is anything with a public parameterless constructor as constrained by:

where T : new()

To instantiate it, provide a type for T that has a public parameterless constructor:

var myRowToObject = new RowToObject<AnotherClass>();

As for how it's intended to be used in your code-base, no idea! :-)

like image 175
Adam Houldsworth Avatar answered May 20 '23 16:05

Adam Houldsworth


RowToObject is a generic class, it takes a type in parameter. This type is identified by T and the constraint (T : new()) enforces that this type must have a default constructor.

More on constraints.

like image 26
vc 74 Avatar answered May 20 '23 14:05

vc 74