0

Here T could be an array or a single object. How can add the array to an arraylist or add a single object to the same arraylist. This gives me a build-time error that the overloaded match for AddRange has invalid arguments.

T loadedContent;
if (typeof(T).IsArray)
{
    contentArrayList.AddRange(loadedContent);
}
else
{
    contentArrayList.Add(loadedContent);
}
4
  • possible duplicate of: stackoverflow.com/q/2788636/210709 Commented Apr 27, 2011 at 19:03
  • 3
    what is the type of contentArrayList? Commented Apr 27, 2011 at 19:08
  • 1
    I removed the XNA tag because your question is really about C#. But on the XNA front, it is generally very unusual to need to make a "list" of content that you have loaded. Just let ContentManager deal with it. Commented Apr 28, 2011 at 5:10
  • (And on the C# front: this is a fairly flagrant non-use of the typing system. I could give you an answer of how you could do this... but you probably shouldn't be doing this in the first place.) Commented Apr 28, 2011 at 5:17

3 Answers 3

0

EDIT: Corrected my answer after checking some of the rules around casting to type Array.

All specific types of arrays, such as int[], string[], or MyCustomObject[] derive from the base Array class, and as such, they implement the ICollection interface, which is what the ArrayList.AddRange method accepts as a parameter.

Assuming that your contentArrayList variable is an ArrayList object, you should be able to cast your loadedContent variable to ICollection:

contentArrayList.AddRange((ICollection)loadedContent)

Alternatively, you could combine the check for whether it is an array with the cast:

Array loadedContentAsArray = loadedContent as Array;
if (loadedContentAsArray != null)
{
    contentArrayList.AddRange(loadedContentAsArray);
}

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

1 Comment

Indeed you could use a Type Constraint on the T parameter to ensure it implements ICollection Example: where T : ICollection
0

The solution provided by Dr. Wily's Apprentice will work but I would like to make some side comments.

You must have design issue in your code if you use generics but you are still committed to a specific data type. Basically you destroy the purpose of the generics, as stated on MSDN:

Generics allow you to define type-safe data structures, without committing to actual data types.

Maybe you should reconsider some refactoring, maybe by adding methods with different parameters or something...

1 Comment

I'm not commited to a specific type, but rather either an array of objects or a single object. This is because LoadContent returns either of these depending on the parameter (single or [])
0

If your contentList is of Type ArrayList, I would go that way.

        ICollection contentArray = loadedContent as ICollection;
        if (contentArray != null)
            contentList.AddRange(contentArray);
        else
            contentList.Add(loadedContent);

Comments

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.