閱讀英文

共用方式為


Delegate.Combine 方法

定義

串連指定多播(可結合)委派的調用清單。

多載

Combine(Delegate[])

串連委派陣列的調用清單。

Combine(ReadOnlySpan<Delegate>)

串連委派範圍的調用清單。

Combine(Delegate, Delegate)

串連兩個委派的調用清單。

Combine(Delegate[])

來源:
Delegate.cs
來源:
Delegate.cs
來源:
Delegate.cs

串連委派陣列的調用清單。

C#
public static Delegate Combine(params Delegate[] delegates);
C#
public static Delegate? Combine(params Delegate?[]? delegates);
C#
public static Delegate Combine(Delegate[] delegates);
C#
[System.Runtime.InteropServices.ComVisible(true)]
public static Delegate Combine(params Delegate[] delegates);

參數

delegates
Delegate[]

要結合的委派陣列。

傳回

具有調用清單的新委派,會串連 delegates 陣列中委派的調用清單。 如果 delegatesnull,則傳回 null;如果 delegates 包含零個專案,或 delegates 中的每個專案 null則傳回 。

屬性

例外狀況

delegates 中的所有非 Null 專案都是相同委派類型的實例。

備註

如果 delegates 數位含 null的專案,則會忽略這些專案。

調用清單可以包含重複的專案;也就是說,參考相同物件上相同方法的專案。

注意

因為變異數而與指派相容的泛型委派不一定可以結合。 若要可組合,類型必須完全相符。 例如,假設名為 Derived 的類別衍生自名為 Base的類別。 類型 Action<Base> 的委派(在 Visual Basic 中Action(Of Base))可以指派給類型 Action<Derived>的變數,如 共變數和反變數中所述,但無法合併兩個委派,因為類型不完全相符。

Combine 有助於建立每次事件發生時呼叫多個方法的事件處理程式。

另請參閱

適用於

.NET 10 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Combine(ReadOnlySpan<Delegate>)

串連委派範圍的調用清單。

C#
public static Delegate? Combine(scoped ReadOnlySpan<Delegate?> delegates);

參數

delegates
ReadOnlySpan<Delegate>

要合併的委派範圍。

傳回

具有調用清單的新委派,會串連 delegates 範圍中委派的調用清單。 如果 delegatesnull,則傳回 null;如果 delegates 包含零個專案,或 delegates 中的每個專案 null則傳回 。

適用於

.NET 10 和 .NET 9
產品 版本
.NET 9, 10

Combine(Delegate, Delegate)

來源:
Delegate.cs
來源:
Delegate.cs
來源:
Delegate.cs

串連兩個委派的調用清單。

C#
public static Delegate Combine(Delegate a, Delegate b);
C#
public static Delegate? Combine(Delegate? a, Delegate? b);

參數

a
Delegate

叫用清單優先的委派。

b
Delegate

叫用清單最後一次的委派。

傳回

具有叫用清單的新委派,會依該順序串連 ab 的調用清單。 如果 bnull,則傳回 a,如果 a 為 null 參考,則傳回 b,如果 ab 都是 null 參考,則傳回 null 參考。

例外狀況

ab 都不是 nullab 不是相同委派類型的實例。

備註

調用清單可以包含重複的專案;也就是說,參考相同物件上相同方法的專案。

注意

因為變異數而與指派相容的泛型委派不一定可以結合。 若要可組合,類型必須完全相符。 例如,假設名為 Derived 的類別衍生自名為 Base的類別。 類型 Action<Base> 的委派(在 Visual Basic 中Action(Of Base))可以指派給類型 Action<Derived>的變數,如 共變數和反變數中所述,但無法合併兩個委派,因為類型不完全相符。

Combine 有助於建立每次事件發生時呼叫多個方法的事件處理程式。

另請參閱

適用於

.NET 10 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0