As we all know, once an array is defined, for example, four lengths, an array is required when an element needs to be added to it. to improve code reuse, simple encapsulation is easy to use. The Code is as follows:
/// <Summary> /// array add /// </Summary> /// <typeparam name = "T"> generic </typeparam> /// <Param name = "array"> array </param> /// <Param name = "item"> items to be added </param> /// <returns> return a new array </returns> Public static T [] add <t> (this t [] array, t item) {int _ COUNT = array. length; array. resize <t> (ref array, _ count + 1); array [_ count] = item; return array ;} /// <summary> /// array add /// </Summary> /// <typeparam name = "T"> generic </typeparam> /// <Param name = "sourcearray"> array </param> /// <Param name = "addarray"> array </param> /// <returns> Returns a new array </returns> Public static T [] addrange <t> (this t [] sourcearray, T [] addarray) {int _ COUNT = sourcearray. length; int _ addcount = addarray. length; array. resize <t> (ref sourcearray, _ count + _ addcount); // foreach (t in addarray) // {// sourcearray [_ count] = T; // _ count ++; //} addarray. copyto (sourcearray, _ count); Return sourcearray ;}
Test code:
[TestMethod()] public void AddTest() { int[] _source = new int[3] { 1, 2, 3 }; int[] _expected = new int[4] { 1, 2, 3, 4 }; _source = _source.Add(4); CollectionAssert.AreEqual(_source, _expected); } [TestMethod()] public void AddRangeTest() { int[] _source = new int[3] { 1, 2, 3 }; int[] _expected = new int[6] { 1, 2, 3, 4, 5, 6 }; _source = _source.AddRange(new int[3] { 4, 5, 6 }); CollectionAssert.AreEqual(_source, _expected); }
Test results:
The Code logic is very simple. It does not need to be explained, but it will be used later. This is based on. NET 2.0, as we all know, in. net 3.0 + with the emergence of LINQ, the implementation of this effect is simpler, the Code is as follows:
/// <Summary> /// array add /// </Summary> /// <typeparam name = "T"> generic </typeparam> /// <Param name = "array"> array </param> /// <Param name = "item"> items to be added </param> /// <returns> return a new array </returns> Public static T [] add <t> (this t [] array, t item) {array =
Array. Concat
<T> (New T [1] {item }). toarray (); Return array ;} /// <summary> /// array add /// </Summary> /// <typeparam name = "T"> generic </typeparam> /// <Param name = "sourcearray"> array </param> /// <Param name = "addarray"> array </param> /// <returns> Returns a new array </returns> Public static T [] addrange <t> (this t [] sourcearray, T [] addarray) {sourcearray =
Sourcearray. Concat
<T> (addarray). toarray (); Return sourcearray;} the same test code:
[TestMethod()] public void AddTest() { int[] _source = new int[3] { 1, 2, 3 }; int[] _expected = new int[4] { 1, 2, 3, 4 }; _source = _source.Add(4); CollectionAssert.AreEqual(_source, _expected); } [TestMethod()] public void AddRangeTest() { int[] _source = new int[3] { 1, 2, 3 }; int[] _expected = new int[6] { 1, 2, 3, 4, 5, 6 }; _source = _source.AddRange(new int[3] { 4, 5, 6 }); CollectionAssert.AreEqual(_source, _expected); }
Test results:
Hope this is helpful!