Return a new array with the value added to the end. Slow and best suited to long lived arrays with few writes relative to reads. - CSharp System

CSharp examples for System:Array Create

Description

Return a new array with the value added to the end. Slow and best suited to long lived arrays with few writes relative to reads.

Demo Code

// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Linq;
using System.Diagnostics.Contracts;
using System.Collections.ObjectModel;

public class Main{
        /// <summary>
        /// Return a new array with the value added to the end. Slow and best suited to long lived arrays with few writes relative to reads.
        /// </summary>
        public static T[] AppendAndReallocate<T>(this T[] array, T value)
        {// w w  w  .j av a  2  s .  com
            Contract.Assert(array != null);

            int originalLength = array.Length;
            T[] newArray = new T[originalLength + 1];
            array.CopyTo(newArray, 0);
            newArray[originalLength] = value;
            return newArray;
        }
}

Related Tutorials