0

In my class, I want to use a dictionary with the following declaration:

Dictionary<string, T[]>

Since the operations of my class are exactly the same for all generic types, I do not wish to define my class as generic (which means I would have to create a separate instance of my class for each generic type I insert into the dictionary ?).

One alternative I'm attempting is to use Dictionary<string, object> instead:

public void Add<T>(string str, T value)
{
  // Assuming key already exists
  var array = (T[]) dictionary[str];
  array[0] = value;
}

However, when iterating over the dictionary, how do I cast the object value back to an array ?

foreach(string strKey in dictionary.Keys)
{
  var array = (T[]) dictionary[strKey];   // How to cast here ?
  //...
  array[0] = default(T);
}

Thanks.

5
  • 1
    Would be great to know what you are trying to do. Commented Jan 9, 2011 at 22:36
  • What does an "cast an object back to its generic type" mean? Commented Jan 9, 2011 at 22:39
  • Where does indexInArray come from? Commented Jan 9, 2011 at 22:39
  • 1
    You have to add some code that shows how.where you want to introduce <T> Commented Jan 9, 2011 at 22:40
  • The dictionary is used in a buffer object. When it's time to flush the buffer, I want to iterate over the dictionary and read all values in all arrays regardless of the type (all value types btw) Commented Jan 9, 2011 at 22:43

3 Answers 3

2

You don't cast to T[], you use System.Array, the base class for all arrays.

var dictionary = new Dictionary<string, Array>();
foreach( var item in dictionary )
    item.Value.SetValue(null, 0);

Other operations you can do without having to cast to a specific array type are Array.Clear and Buffer.BlockCopy.

Sign up to request clarification or add additional context in comments.

4 Comments

+1: Nice. I was about to say "this won't work for value-types", but then I saw this: "If SetValue is used to assign null to an element of an array of value types, all fields of the element are initialized to zero." msdn.microsoft.com/en-us/library/kk3bwkb0.aspx
@Ani: Exactly. Although that did have me worried for a moment while I was thinking about the answer.
Thanks. For the general case, how can I reset to default of T: item.Value.SetValue(default(T), 0) ?
@alhazen: This behaves as you expect - it should be the same as having used a default(T), (assuming you knew T at compile-time of course).
2

Since the operations of my class are exactly the same for all generic types

What you can do, is define all the methods which are the same in an interface and make your generic classes implement that interface. Once you do that, all that you need to do is to create a dictionary which takes Strings as keys and arrays of type InterfaceName. This should allow you to have generic values (instead of the object) and you will not need to type cast either.

2 Comments

In my case, T is always a value type, i.e. the dictionary value is usually int[] or float[]
"operations are the same" appears to be talking about operations on the collection, not the operations on elements of the arrays inside the collection.
1

You should use a generic class. The advantage of using a generic class if that the object retrieved from the dictionary is type-safe and does not require casting - it will already be of the required type.

You say that 'the operations of my class are exactly the same for all generic types' - if that means what I think it means then it is merely reinforcing the usefulness of a generic class in your case.

2 Comments

The way I read the question, he's got a collection of arrays. The arrays are not all the same type, so making the containing collection generic doesn't help.
@Ben Voigt - Interesting, I don't read that but I see what you mean. I was focusing on the declaration Dictionary<string, T[]>, (not the same as Dictionary<string, object[]>)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.