I have to make a decision between object-based and generics.
I have a data structure and it can be int, string or bool. Later I need a list of items and I'm not sure if i should make Item generic or hold the value in item as object.
public class Item<T>
{
private T value = default(T);
public Item(T value)
{
this.value = value;
}
public T Value
{
get { return this.value; }
set { this.value = value; }
}
}
or
public class Item
{
private Object value = null;
public Item(Object value)
{
this.value = value;
}
public Object Value
{
get { return this.value; }
set { this.value = value; }
}
}
Actually, I wanted to use generics but I had problems by getting the type of the variable.
Item<T> item;
switch(type) // type is a string
{
case "int":
item = new Item<Int32>;
break;
case "string":
item = new Item<String>;
break;
case "bool":
item = new Item<Boolean>;
break;
}
Does anyone have any advice for me?
EDIT: Sorry I forgot one fact: I need to decide the type at runtime. I get a string input by which i have to identify the type!