0

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!

  • 6
    If you are going to do a `switch` on the generic _type_, you might as well not use generics. – Oded Apr 02 '12 at 08:55
  • Where does the `type` come from? – svick Apr 02 '12 at 09:05
  • 1
    Do you want a list which can contain `Item` **and** `Item` or will you only have 'pure' lists only containing one type of items (i.e. only `Item`s)? – ChrisWue Apr 02 '12 at 09:05
  • i get a string and i have to decide if it is a number, text or bool. pure lists would be enough then one way could be that I omit the temp variable and direclty add one to the corresponding list. –  Apr 02 '12 at 09:20
  • Why are you doing this? The best solution depends somewhat on the answer. Are you sure there will never be more than 3 possible types? Do you ever need to treat the items as a single homogeneous collection? Do you even need the `Item` class? (That is, why not just store strings as strings, ints as ints, and bools as bools)? – phoog Apr 02 '12 at 20:28
  • I'm writing an interpreter for an abstract syntax tree. and so there cant be more types. –  Apr 03 '12 at 07:15

2 Answers2

0

If you're going to use a Reflection or DataBinding (where you may want to have generic collection of elements as much as it possible) that heavily works on that data structure, I would personally avoid use of generics, cause the story becomes harder when you use them.

In all other cases it's better to use generics.

So considering the information I have by reading your question, I would say: use generics.

Tigran
  • 61,654
  • 8
  • 86
  • 123
0

If you use the "object-based" approach, you would need to cast your items every time. If you use generics, you don't do that and that's the main reason for using generics in the first place.

However, I'm not sure what problems you have while getting the type with generics, when it's really easy.

Please, look at this link, that should solve it for you: How to get the type of T from a member of a generic class or method?

Anyway, you have to decide, what exactly is your goal and which approach will be easier and more effective for you. I'd suggest try both, think about various situations and choose what suits you best.

Please, just don't use switch like that...

Community
  • 1
  • 1
walther
  • 13,466
  • 5
  • 41
  • 67