Click here to Skip to main content
15,880,405 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
using System;
using System.Collections.Generic;


namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Program dn = new Program();

            int[] array1 = { 1, 2, 3 };
            int[] array2 = { 1, 2, 3 };
            int[] res = dn.merge(array1, array2);

            Console.WriteLine(res);
        }

        public int[] merge(int[] array1, int[] array2)
        {
            int[] array3 = new int[6];
            int[] arr4 = new int[6];
            int i, j;
            int array1length = array1.Length;
            int array2length = array2.Length;
            //array3 = array1.Concat(array2).ToArray();
            for (i = 0; i < array1length; i++)
            {
                array3[i] = array1[i];
            }

            int FinalLength = array1length + array2length;
            for (j = 0; j < array2length; j++)
            {
                i++;
                array3[(i - 1)] = array2[j];

            }
            for (int k = 0; k <= arr4.Length; k++)
            {
                arr4 = array3;
            }
            return arr4;
        }
    }
}


What I have tried:

I have debug the code and it is returning the exact output i.e the values of two arrays as a merged array.But on console screen it is showing "system.int32[]".
Posted
Updated 22-Aug-17 20:04pm

The problem you are seeing is because the text written to the console with Console.WriteLine() is the result of calling ToString() on the input object. So to fix the problem all you need to do is convert the array to a string yourself. One way of doing this is to use string.Join(). If you decide to use that method then you would replace:
C#
Console.WriteLine(res);
with:
C#
Console.WriteLine (string.Join(", ", res));

Does that make sense?
 
Share this answer
 
v2
Comments
George Swan 23-Aug-17 1:09am    
Shouldn't the replacement be Console.WriteLine(string.Join(", ".res));
Member 10585568 23-Aug-17 1:52am    
Thanks for identifying my mistake.
Here I have used an int array how will it(i.e string.Join(", ", res)) work with an int array.And I have already joined both the arrays to another array called "res".
It would be helpful if you provide me some way to show the values of that array in console.
George Swan 23-Aug-17 4:33am    
The string.Join method will consecutively convert every int in your array to a string and will join it to the next int using ", " as a separator. You end up with a string similar to this "1, 2, 3" being returned from the method. Console.WriteLine then prints it.
2Programmer78 23-Aug-17 8:46am    
You are right about the replacement, I was not being very careful.
Sorry sir, as I made a blunder mistake in my code. I have retified that and now it is showing the exact output.

The change are given below :

for (int i = 0; i <= res.Length-1; i++)
     {
         Console.WriteLine("{0}",res[i]);
     }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900