How to make generic List<T> for bool, Int16 , string in the following code?
TypeScript
class myClass
{
string _dataType;
public List<Object> _list;
public myClass(string dataType)
{
_dataType=dataType;
if (dataType=="bool")
_list=new List<bool>();
else if (dataType=="int16")
_list=new List<Int16>();
else if (dataType == "string")
_list=new List<string>();
else
_list = new List<Object>();
}
}Error: Cannot implicitly convert type 'System.Collections.Generic.List<bool>' to 'System.Collections.Generic.List<object>'
Mike GoldRank #50 · 30.6K pointsPosted Mar 26, 2007, 10:58 AM EDT
best
-Mike
Sokak37 pointsPosted Mar 28, 2007, 6:18 PM EDT
Eg:
public class myClass <T>
{
private List<T> _list = new List<T>();
// Implement your code here using "T" as your type where needed rather than worrying about hard-coded types.
}
You can also restrict the types that your class supports.
Eg: To restrict it to Int, Bool, or String
public class myClass <T> where T : int, bool, string