What does T mean in this class signature and how can I create this class?

I look at someone from my professor code for him while he is at Holiday, and I stumbled upon this unacceptable class. Can someone explain what T represents and possibly how to instantiate a class such as this?

public class RowToObject<T> where T : new() 
+4
source share
5 answers

T is anything with an open constructor without parameters:

where T : new()

To create an instance, specify the type T , which has an open constructor without parameters:

var myRowToObject = new RowToObject<AnotherClass>();

As for how it is intended for use in your code base, I don’t know! :-)

+6
source

RowToObject is a general class, it takes a type parameter. This type is identified by T, and the restriction (T: new ()) establishes that this type must have a default constructor.

Learn more about restrictions.

+4
source

This is the Generic class. It takes a type inside the corner brackets, like this RowToObject<SomeType> . The Where bit acts as a filter, which says that the passed type must have an open constructor without any parameters on it.

+1
source

Assuming RowToObject has a constructor without parameters, you will create it something like this:

 new RowToObject<object>(); 

T means that you can put in it any class that meets the criteria "has a constructor without parameters" that System.Object has.

Perhaps another object is needed. This will depend on how the class is used.

+1
source

Can someone explain what T represents and maybe how to instantiate a class like this?

T Type

 RowToObject<int> Example = new RowToObject<int>(); 

You really need to look into your class notes to understand my answer.

This is how you state whether the class will be universal. While T will accept any class, do not confuse the Type class; there is a reason (which I cannot explain) uses the T syntax

+1
source

Source: https://habr.com/ru/post/1386367/


All Articles