locked
Inserting a few elements into a generic array RRS feed

  • Question

  • Is there a ready method in the BCL to allow me to insert an array into another array, eg:  I have:

    int[10] myArray;
    int[3] toInsert;

    Now I want to insert the elements of toInsert into myArray at, say, element 5 onwards.  My final array would contain:

    a. Elements 0 through 4: myArray[0] through myArray[4]
    b. Elements 5 through 7: toInsert[0] through toInsert[2]
    c. Elements 8 through 12: myArray[5] through myArray[9]

    It has to work for arrays of any type.

    Thanks.
    Friday, April 24, 2009 4:37 AM

Answers

  • Hi Kong,

    There is no ready method in BCL for this, but you can achieve this be extenstion method in .NET.

     Write the following class in your assembly
        internal static class extensionClass
        {
            public static void insertArray(this Array dArray, int insertAtIndex, Array arrayToInsert) {
                if (insertAtIndex < 0 || insertAtIndex >= dArray.Length)
                    throw new Exception("The given insert index will result in array index out of range exception");
                if (dArray.Length < arrayToInsert.Length)
                    throw new Exception("The Array given to insert should contain the less amount than the destination array");
                if(insertAtIndex + arrayToInsert.Length >= dArray.Length)
                    throw new Exception("The insertion of given array at the given insert index will result in array index out of range exception. Please given an array with lesser elements");
                for(int count = 0; count < arrayToInsert.Length; count ++)
                 dArray.SetValue(arrayToInsert.GetValue(count),insertAtIndex++);
            }
        }
    Then you can insert the array in any type of array as follows.

    int[] dArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int[] sArray = new int[] { 11, 12, 13 };
    
    dArray.insertArray(4, sArray);
    
    //The result will be 1, 2, 3, 4, 11, 12, 13, 8, 9, 10
    For more details on Extension methods, please refer

    http://msdn.microsoft.com/en-us/library/bb383977.aspx

    Thanks and regards,
    Vijay Pandurangan

    • Marked as answer by K.Kong Friday, April 24, 2009 6:37 AM
    Friday, April 24, 2009 5:22 AM

All replies

  • Hi Kong,

    There is no ready method in BCL for this, but you can achieve this be extenstion method in .NET.

     Write the following class in your assembly
        internal static class extensionClass
        {
            public static void insertArray(this Array dArray, int insertAtIndex, Array arrayToInsert) {
                if (insertAtIndex < 0 || insertAtIndex >= dArray.Length)
                    throw new Exception("The given insert index will result in array index out of range exception");
                if (dArray.Length < arrayToInsert.Length)
                    throw new Exception("The Array given to insert should contain the less amount than the destination array");
                if(insertAtIndex + arrayToInsert.Length >= dArray.Length)
                    throw new Exception("The insertion of given array at the given insert index will result in array index out of range exception. Please given an array with lesser elements");
                for(int count = 0; count < arrayToInsert.Length; count ++)
                 dArray.SetValue(arrayToInsert.GetValue(count),insertAtIndex++);
            }
        }
    Then you can insert the array in any type of array as follows.

    int[] dArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int[] sArray = new int[] { 11, 12, 13 };
    
    dArray.insertArray(4, sArray);
    
    //The result will be 1, 2, 3, 4, 11, 12, 13, 8, 9, 10
    For more details on Extension methods, please refer

    http://msdn.microsoft.com/en-us/library/bb383977.aspx

    Thanks and regards,
    Vijay Pandurangan

    • Marked as answer by K.Kong Friday, April 24, 2009 6:37 AM
    Friday, April 24, 2009 5:22 AM
  • Thanks.  This is very useful.  But it needs .NET Framework 3.5.
    Friday, April 24, 2009 6:34 AM
  • Yes, but even in .NET 3.0 you can created the extenstion methods. But you can compile your assembly with Extension method for .Net framework 2.0 (compiling for the specific target http://msdn.microsoft.com/en-us/library/bb398202.aspx). To do this, all you need is VS2008, see http://www.codethinked.com/post/2008/02/Using-Extension-Methods-in-net-20.aspx  and http://kohari.org/2008/04/04/extension-methods-in-net-20/ for more detail.

    Thanks and Regards,
    Vijay Pandurangan
    Friday, April 24, 2009 6:41 AM