|
本文章是由機器翻譯。 將指標移到文章內的文字上方即可查看原文。 其他資訊。
|
譯文
原文
|
Enumerable.Union<TSource> 方法 (IEnumerable<TSource>, IEnumerable<TSource>)
命名空間: System.Linq
組件: System.Core (在 System.Core.dll 中)
public static IEnumerable<TSource> Union<TSource>( this IEnumerable<TSource> first, IEnumerable<TSource> second )
型別參數
- TSource
輸入序列的項目之型別。
參數
- first
- 型別:System.Collections.Generic.IEnumerable<TSource>
IEnumerable<T> ,其獨特項目構成第一個等位集合。
- second
- 型別:System.Collections.Generic.IEnumerable<TSource>
IEnumerable<T> ,其獨特項目構成第二個等位集合。
使用注意事項
在 Visual Basic 和 C# 中,您可以在任何 IEnumerable<TSource> 型別物件中呼叫這個方法以做為執行個體。使用執行個體方法語法呼叫這個方法時,請省略第一個參數。如需詳細資訊,請參閱擴充方法 (Visual Basic)或擴充方法 (C# 程式設計手冊)。| 例外狀況 | 條件 |
|---|---|
| ArgumentNullException |
public class Product : IEquatable<Product> { public string Name { get; set; } public int Code { get; set; } public bool Equals(Product other) { //Check whether the compared object is null. if (Object.ReferenceEquals(other, null)) return false; //Check whether the compared object references the same data. if (Object.ReferenceEquals(this, other)) return true; //Check whether the products' properties are equal. return Code.Equals(other.Code) && Name.Equals(other.Name); } // If Equals() returns true for a pair of objects // then GetHashCode() must return the same value for these objects. public override int GetHashCode() { //Get hash code for the Name field if it is not null. int hashProductName = Name == null ? 0 : Name.GetHashCode(); //Get hash code for the Code field. int hashProductCode = Code.GetHashCode(); //Calculate the hash code for the product. return hashProductName ^ hashProductCode; } }
Product[] store1 = { new Product { Name = "apple", Code = 9 },
new Product { Name = "orange", Code = 4 } };
Product[] store2 = { new Product { Name = "apple", Code = 9 },
new Product { Name = "lemon", Code = 12 } };
...
//Get the products from the both arrays
//excluding duplicates.
IEnumerable<Product> union =
store1.Union(store2);
foreach (var product in union)
Console.WriteLine(product.Name + " " + product.Code);
/*
This code produces the following output:
apple 9
orange 4
lemon 12
*/
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (不支援伺服器核心角色), Windows Server 2008 R2 (SP1 (含) 以後版本支援伺服器核心角色,不支援 Itanium)
.NET Framework 並不支援各種平台的所有版本。如需支援的版本的清單,請參閱.NET Framework 系統需求。