How To Merge Two/Four/Six Arrays in ASP.NET?

With Duplicates

     

private string[] MergeArray(string[] DestinationArray, string[] SourceArray)

        {

            int iSourceIndex;

            int iDestinationLength = DestinationArray.Length;

            if (SourceArray.Length > 0)

                Array.Resize(ref DestinationArray, DestinationArray.Length + SourceArray.Length);

            for (iSourceIndex = 0; iSourceIndex < SourceArray.Length ; iSourceIndex++)

            {

                DestinationArray[iDestinationLength + iSourceIndex] = SourceArray[iSourceIndex];

            }

            return DestinationArray;

        }

 

Without Duplicates

    private string[] MergeArray(string[] DestinationArray, string[] SourceArray)

        {

            int iSourceIndex;

            int iDestinationLength = DestinationArray.Length;

            if (SourceArray.Length > 0)

                Array.Resize(ref DestinationArray, DestinationArray.Length + SourceArray.Length);

            for (iSourceIndex = 0; iSourceIndex < SourceArray.Length ; iSourceIndex++)

            {

                if (Array.IndexOf(arrDestination, arrSource[iSourceLength]) == -1)

                        DestinationArray[iDestinationLength + iSourceIndex] = SourceArray[iSourceIndex];

            }

            return DestinationArray;

        }

    }


Array.IndexOf will do a search for the specified object on array and returns the index of the first occurrence. it will return -1 if it is not find anything on the array .

Usage

        string[] one=new string[]{"A","B"};

        string[] two = new string[] { "C", "D" };

        string[] three = new string[] { "E", "F" };

        string[] four = new string[] { "G", "H", "I" };

      

        //Merge Two Arrays

        one = MergeArray(one, two);

        //Merge Four Arrays

        one = MergeArray(MergeArray(one, two), MergeArray(three, four));

 

        for(int i=0;i<one.Length;i++)

            Response.Write(one[i]);


 

Alternate Titles: How To Merge Two Arrays in ASP.NET,How To Merge Two Arrays in C#, Resize a array, Merge without duplicates,How To Merge Four Arrays in ASP.NET, How To Merge 6 Arrays in ASP.NET