0

This is the code:

class Program
{
    void Add2Array(object arr, object item)
    {
        if (arr.GetType() == typeof(string[]))
        {
            int iLen = (arr as Array).Length;
            var c = Array.CreateInstance(typeof (String), 3);
            Array v = Array.CreateInstance((arr as Array).GetValue(0).GetType(), iLen+1); // this works but when if arr is empty it wont work
            Array.Copy(ar, v, iLen);
            v.SetValue(item, iLen);
        }
    }
    public string[] sarr = new string[1];

    static void Main(string[] args)
    {
        Program p = new Program();
        p.sarr[0] = "String Item";
        p.Add2Array(p.sarr, "New string item");
    }
}

I want to create a method which can take every type of arrays and put new item into them. Above code is my solution (if you know better please share) and if arr parameter hasn't any item, it won't work properly. Because if I use this Array.CreateInstance(arr.GetType(),3) it will create new array like this v.GetType() => string[2][] because arr is string array and if i create with same type it is returning two dimensonal array.

How can I extend an array(given as a parameter) and put new item into it ?

1
  • Is there a reason, why you don't use a List object? Commented Dec 21, 2011 at 21:31

2 Answers 2

5
T[] Add2Array<T>(T[] arr, T item)
{
   return arr.Concat(new[]{item}).ToArray();
}
Sign up to request clarification or add additional context in comments.

3 Comments

I like it! Very concise! Of course it throws if arr is null, but I'd expect that, not sure if OP does though or if he'd want a new array of size 1.
I would let it throw rather than convert null to an empty array, or array with 1 item. null and new T[0] are semantically different
Oh very true, as I said that's the behavior I'd expect. Just want to make sure the OP understands the difference in case he expected a different outcome.
3

Array cannot be extended. The only thing you could do is to copy data to a new array which is bigger than original one and append data.

BTW, why not to use List<>?

using System;

namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {
            int[] x = new int[]{2,3,5};
            int[] y = new ArrayExpander().AddItem(x, 0);

            foreach (var i in y)
            {
                Console.Write(i);
            }
        }
    }

    class ArrayExpander
    {
        public T[] AddItem<T>(T[] source, T item)
        {
            var destination = new T[source.Length + 1];
            Array.Copy(source, destination, source.Length);
            destination[source.Length] = item;
            return destination;
        }
    }
}

1 Comment

This is good answer but as a COM object ArrayExpander's generic method didn't seen. Why can't I see this method from COM registration?

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.