CSharp examples for System:Array Create
Create an array that is the union of all members in a, with all members of b that are not already in a.
using System.Linq; using System.Collections.Generic; using System;// w w w .ja v a 2 s . c o m public class Main{ /// <summary> /// Create an array that is the union of all members in a, with all members of b that are not already in a. /// </summary> public static T[] Union<T>(this T[] a, T[] b) { if (a == b) return a; if (a.Length == 0) return b; if (b.Length == 0) return a; if (a.Length == 1) { if (b.Length == 1) return a[0].Equals(b[0]) ? a : new[] { a[0], b[0] }; return b.Union(a[0]); } if (b.Length == 1) return a.Union(b[0]); return Enumerable.Union(a, b).ToArray(); } /// <summary> /// Create an array that is the union of all members in a, with b added if b is not an element of a. /// </summary> public static T[] Union<T>(this T[] a, T b) { if (a.Length == 0) return new[] { b }; if (Array.IndexOf(a, b) >= 0) return a; var res = new T[a.Length + 1]; Array.Copy(a, 0, res, 0, a.Length); res[res.Length - 1] = b; return res; } }