Concatenates contents of two arrays, producing a new one. - CSharp System

CSharp examples for System:Array Create

Description

Concatenates contents of two arrays, producing a new one.

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*  w  w w  . j a v a  2 s .c  om*/

public class Main{
        /// <summary>
    ///   <para>Concatenates contents of two arrays, producing a new one.</para>
    /// </summary>
    /// <typeparam name="T">Type of array elements.</typeparam>
    /// <param name="self">First (left-side) array.</param>
    /// <param name="other">Second (right-side) array.</param>
    /// <returns>Results array which contains all elements from <paramref name="self"/> array, immediately followed by all elements from <paramref name="second"/> array.</returns>
    /// <exception cref="ArgumentNullException">If either <paramref name="self"/> or <paramref name="other"/> is a <c>null</c> reference.</exception>
    /// <seealso cref="Array.Copy(Array, Array, int)"/>
    public static T[] Join<T>(this T[] self, T[] other)
    {
      Assertion.NotNull(self);
      Assertion.NotNull(other);

      var result = new T[self.Length + other.Length];
      Array.Copy(self, result, self.Length);
      Array.Copy(other, 0, result, self.Length, other.Length);
      return result;
    }
        /// <summary>
    ///   <para>Concatenates all elements in a sequence into a string, using specified separator.</para>
    /// </summary>
    /// <typeparam name="T">Type of elements in a sequence.</typeparam>
    /// <param name="self">Source sequence of elements.</param>
    /// <param name="separator">String to use as a separator between concatenated elements from <paramref name="self"/>.</param>
    /// <returns>String which is formed from string representation of each element in a <paramref name="self"/> with a <paramref name="separator"/> between them.</returns>
    /// <exception cref="ArgumentNullException">If either <paramref name="self"/> or <paramref name="separator"/> is a <c>null</c> reference.</exception>
    public static string Join<T>(this IEnumerable<T> self, string separator)
    {
      Assertion.NotNull(self);
      Assertion.NotNull(separator);

      var sb = new StringBuilder();
      self.Each(element => sb.Append($"{element}{separator}"));
      if (sb.Length > 0)
      {
        sb.Remove(sb.Length - separator.Length, separator.Length);
      }
      return sb.ToString();
    }
        /// <summary>
    ///   <para>Iterates through a sequence, calling a delegate for each element in it.</para>
    /// </summary>
    /// <typeparam name="T">Type of elements in a sequence.</typeparam>
    /// <param name="self">Source sequence for iteration.</param>
    /// <param name="action">Delegate to be called for each element in a sequence.</param>
    /// <returns>Back reference to the current sequence.</returns>
    /// <exception cref="ArgumentNullException">If either <paramref name="self"/> or <paramref name="action"/> is a <c>null</c> reference.</exception>
    public static IEnumerable<T> Each<T>(this IEnumerable<T> self, Action<T> action)
    {
      Assertion.NotNull(self);
      Assertion.NotNull(action);

      foreach (var value in self)
      {
        action(value);
      }

      return self;
    }
        /// <summary>
    ///   <para>Sequentially removes all elements, returned by the enumerator, from the specified collection, if it has it.</para>
    /// </summary>
    /// <typeparam name="T">Type of collection's elements.</typeparam>
    /// <param name="self">Collection from which elements are removed.</param>
    /// <param name="other">Elements enumerator that provider elements for removal from the collection <see cref="self"/>.</param>
    /// <exception cref="ArgumentNullException">If either <paramref name="self"/> or <paramref name="other"/> is a <c>null</c> reference.</exception>
    /// <seealso cref="ICollection{T}.Remove(T)"/>
    /// <seealso cref="Add{T}(ICollection{T}, IEnumerable{T})"/>
    public static ICollection<T> Remove<T>(this ICollection<T> self, IEnumerable<T> other)
    {
      Assertion.NotNull(self);
      Assertion.NotNull(other);

      foreach (var item in other)
      {
        self.Remove(item);
      }

      return self;
    }
}

Related Tutorials