0

Please how can we overload the "+" Operator for simple array joining convenience?

How can we define an extension to simplify this as below:

    Dim a = {1, 2}
    Dim b = {3, 4}
    Dim c = a + b ' should give {1, 2, 3, 4}

I get the error below:

'Error BC30452  Operator '+' is not defined for types 'Integer()' and 'Integer()'
Charles Okwuagwu
  • 10,538
  • 16
  • 87
  • 157

1 Answers1

1

Duplicate of Overloading the + operator to add two arrays.

So since this is not possible (apart from using an extension) you could use this simple workaround using LINQ:

Dim c = a.Concat(b).ToArray()

Here's a possible implementation for an extension that works with arrays and lists as input and is more efficient than the LINQ approach:

<System.Runtime.CompilerServices.Extension()> _
Public Function Plus(Of t)(items1 As IList(Of T), items2 As IList(Of T)) As T()
    Dim resultArray(items1.Count + items2.Count - 1) As t
    For i As Int32 = 0 To items1.Count - 1
        resultArray(i) = items1(i)
    Next
    For i As Int32 = 0 To items2.Count - 1
        resultArray(i + items1.Count) = items2(i)
    Next
    Return resultArray
End Function

You can use it in this way:

Dim a = {1, 2}
Dim b = {3, 4}
Dim c = a.Plus(b)
Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Please can you show such an extension, i guess that is exactly what i need – Charles Okwuagwu Jan 04 '16 at 09:58
  • It is not a duplicate of the referenced SO post. That question was adding the elements, I need a to "JOIN" the two arrays like you suggested with linq snippet – Charles Okwuagwu Jan 04 '16 at 10:05
  • @CharlesO: but it's not clear what you mean with _join_ in this context. Both arrays are not joined (by what?), you are adding all elements from the first array and all elements from the second array to create a new array that contains both. – Tim Schmelter Jan 04 '16 at 10:11
  • Yes i am concatenating the elements from the second array onto the first to give a new array. Please is it possible to define an operator overload for "+" not a "Plus" method – Charles Okwuagwu Jan 04 '16 at 10:15
  • 1
    @CharlesO: no (as you can read int the duplicate question) since array is not a type that you can modify. You could create a wrapper class that holds an array as field variable and overloads the +-operator to concat elements to it. But i assume that it's also not what you need. – Tim Schmelter Jan 04 '16 at 10:25
  • Seems possible here thought https://smellegantcode.wordpress.com/2014/04/24/adventures-in-roslyn-adding-crazily-powerful-operator-overloading-to-c-6/ – Charles Okwuagwu Jan 04 '16 at 10:30
  • In terms of efficiency, shouldn't you use Array.copy instead of looping? – Charles Okwuagwu Jan 04 '16 at 10:33
  • @CharlesO: it seems that it's a C# feature and only available with C#6(VS 2015). I can't check it since i'm stuck to VS 2010. – Tim Schmelter Jan 04 '16 at 10:36
  • 1
    @CharlesO: You can use `Array.Copy`, but i'm sure that you pretty much never notice a difference. `Copy` also uses loops but skips the bounds checks(so if the array indexer is valid). The drawback of `Array.Copy` is that you can't use this extension for arrays **and** lists anymore but only for arrays. – Tim Schmelter Jan 04 '16 at 10:38