Skip to content

Latest commit

 

History

History
45 lines (35 loc) · 2.56 KB

File metadata and controls

45 lines (35 loc) · 2.56 KB
title Generic Delegates (C# Programming Guide)
ms.date 07/20/2015
ms.prod .net
ms.technology
devlang-csharp
ms.topic article
helpviewer_keywords
generics [C#], delegates
delegates [C#], generic
ms.assetid bdea509c-44c1-4309-aaa9-15c7aee009df
caps.latest.revision 16
author BillWagner
ms.author wiwagn

Generic Delegates (C# Programming Guide)

A delegate can define its own type parameters. Code that references the generic delegate can specify the type argument to create a closed constructed type, just like when instantiating a generic class or calling a generic method, as shown in the following example:

[!code-csharpcsProgGuideGenerics#36]

C# version 2.0 has a new feature called method group conversion, which applies to concrete as well as generic delegate types, and enables you to write the previous line with this simplified syntax:

[!code-csharpcsProgGuideGenerics#37]

Delegates defined within a generic class can use the generic class type parameters in the same way that class methods do.

[!code-csharpcsProgGuideGenerics#38]

Code that references the delegate must specify the type argument of the containing class, as follows:

[!code-csharpcsProgGuideGenerics#39]

Generic delegates are especially useful in defining events based on the typical design pattern because the sender argument can be strongly typed and no longer has to be cast to and from xref:System.Object.

[!code-csharpcsProgGuideGenerics#40]

See Also

xref:System.Collections.Generic
C# Programming Guide
Introduction to Generics
Generic Methods
Generic Classes
Generic Interfaces
Delegates
Generics