[C #] adding extensions to array

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.