0

We have two set of string arrays, e.g.:

string[] a = new string[] {"value1","value2","value3"};
string[] b = new string[] {"value1","value2","value3"}; 

I have to develop a generic method which should takes these two string arrays as parameters and should return the combined one back:

public T[] ArrayItms(T1[] a, T2[] b)
{
    return T[];
}

I got bit confused, doing so in generics.

8 Answers 8

7

You can use LINQ to do that:

public T[] ArrayItms<T>(T[] a, T[] b)
{
    return a.Concat(b).ToArray();
}

Or, doing it manually by copying the arrays:

public T[] ArrayItms<T>(T[] a, T[] b)
{
    T[] result = new T[a.Length + b.Length];
    Array.Copy(a, result, a.Length);
    Array.Copy(b, 0, result, a.Length, b.Length);
    return result;
}

In both cases, beware uninitialized paremeters (not done in the above example codes).

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

9 Comments

Is it just me or would this fail. T1 and T2 aren't defined and aren't convertible to T
Nice! There is so many nice new methods that came along to support LINQ.
@Loren: I’ve fixed it now. Using different argument types might actually make sense, though (if they're somehow convertible into each other). I’ll leave it as it is for now and see what the OP has to say.
This won't work! The compiler's gonna scream at you for not defining T1 or T2 and even then for trying to create a T array out of T1's and T2's!
Yeah, I implemented the convertible approach in my comment
|
6

Arrays are so 2004.

public IEnumerable<string> ArrayItms(IEnumerable<string> a, IEnumerable<string> b)
{
    return a.Concat(b);
}

You can pass your arrays to this function directly, because arrays implement IEnumerable. If you really need an array result, call .ToArray() after calling the function. Or since it's just a one-liner skip the function entirely and just call .Concat() on the first array.

Note that this is very easy to convert to a generic function that will work with any type. But so far you've only said that you care about strings.

Comments

3

Here is a very simple implementation:

public T[] Combine<T>(IEnumerable<T> a, IEnumerable<T> b)
{
    List<T> result = new List<T>(a);

    result.AddRange(b);

    return result.ToArray();
}

Comments

1

Hmm... this doesn't seem to follow proper rules for generics, your array arguments should have the same type, something like this:

public T[] ArrayItems<T>(T[] a, T[] b)
{
   return a.Concat(b).ToArray();
}

Alternatively, you could do something like this:

public T[] ArrayItems<T,T1,T2>(T1[] a, T2[]b) where T1 : T where T2 : T
{
    return a.Select(i => (T)i).Concat(b.Select(i=>(T)i)).ToArray();
}

Comments

1

If both arrays are of the exact same type, you can use:

public static T[] Concatenate<T>(T[] a, T[] b)
{
    if (a == null) throw new ArgumentNullException("a");
    if (b == null) throw new ArgumentNullException("b");

    T[] result = new T[a.Length + b.Length];
    Array.Copy(a, result, a.Length);
    Array.Copy(b, 0, result, a.Length, b.Length);
    return result;
}

Otherwise

public static TResult[] Concatenate<TResult, T1, T2>(T1[] a, T2[] b)
    where T1 : TResult where T2 : TResult
{
    if (a == null) throw new ArgumentNullException("a");
    if (b == null) throw new ArgumentNullException("b");

    TResult[] result = new TResult[a.Length + b.Length];
    Array.Copy(a, result, a.Length);
    Array.Copy(b, 0, result, a.Length, b.Length);
    return result;
}

should do.

EDIT: Maybe Array.Copy() isn't that fast, so it could be benchmarked against LINQ's concat, or a strongly typed version could be custom-made.

2 Comments

Edited my post to answer the question now, please remove downvote.
Didn't see your answer. I deleted min. I'd just add : It works because array are covariants at runtime in C# since version 1. Compile time safety is assured by generic constraints.
0

Do it without generics then replace each occurence of string with T.

Comments

0

Since arrays are IEnumerable, you could use this: http://msdn.microsoft.com/en-us/library/bb302894.aspx.

Comments

0

You can use LINQ to do this, so you should change it to

public T[] ArrayItms<T>(T[] a, T[] b) 
{ 
     return a.Concat(b).ToArray();
}

Note you cannot combine two array of different types (T1, T2 -> T, T) and you have to use MethodName. You should call the method like:

ArrayItems<TypeName>(TypeName a, TypeName b);

1 Comment

Why do you specify the generic type parameter explicitly?

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.