Article ID: 2843733 - View products that this article applies to.
Collapse this tableExpand this table
Delegates are often used in .NET based languages, especially in F#, C# and Visual Basic. The most common use of delegates that correspond directly to .NET delegates are C# and Visual Basic delegates.
LINQ in C# and Visual Basic uses delegates heavily, and although we can use anonymous delegates on the extension methods that part of LINQ, the delegates are actually in the form of Func<T,TResult>.
For example, in C# we can use LINQ Select extension method in the form of Lambda expression (by not using the syntactic sugar of select keyword) to map current running list of Process to only display only a list of process name:
The Visual Basic sample is:
The delegates in F# looks identical in concept. But there's no identical semantic of delegates in F#! F# will use FastFunc instead of Func in C#/Visual Basic.
In the code above, the "fun p-> p.ProcessName" has different meaning because the F# compiler will translate this line:
into this (in C# using decompiler and simplified):
Now, the Seq.Map is translated into calls to SeqModule.Map that has parameters of processNamesu004025 and the processList.
The processNamesu004025 is actually the delegate of F#.
Before going into the delegate of F#, let's look into the signature of SeqModule.Map in the form of C#:
We can deduce that the delegate is typed as FSharpFunc<T,TResult>. But FSharpFunc is an abstract class, therefore it has to be a concrete class.
Let's go back to the code above. The suspect of the delegate is processNamesu004025. We can go further investigate to look at the type signature.
With the help from decompiler, the type is:
Actual implementation of the processNamesu004025 is actually a class itself that derives from FSharpFunc:
This is where the semantic really differs.
Using C#/Visual Basic delegate in F# is simple, with the help of Microsoft F# Powerpack.
This useful library can be downloaded from:
In this library, there is a helper class that provides function for this purpose, the FuncConvertExtensions class. In FuncConvertExtensions we have this method named ToFSharpFunc to provide interop path of converting Func to F# delegates.
This is the ToFSharpFunc signature in C#:
There are also overrides of ToFSharpFunc to use when dealing with other types of Func:
This is the sample case:
The F# code below will return the index of the first found data:
To use this function of GetRowIndex in C#/Visual Basic, we simply call ToFSharpFunc. This is the code in C#:
System.Func delegates (including Func<T,TResults> and more than 2 type parameters Func) usage in F# is simpler than F# delegate in C#/Visual Basic.
Func in C#/Visual Basic can be called directly, but Func has to be instantiated before as F# lambda will always return abstract delegate as long as the type parameter signature is the same.
Let's take LINQ Select as an example.
Using Select in F# needs Func<T,TResult>. So calling Select method in F# will be: (with samples of querying running processes in your computer)
We can now interop easily with other concrete types of delegate in .NET Base class library such as Action<T> and Predicate<T>. Note that Predicate<T> can also be replaced with Func<T,boolean> as it has the same semantics.
For more information, please visit:
Community Solutions Content DisclaimerMICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES.
Article ID: 2843733 - Last Review: May 7, 2013 - Revision: 2.0
Contact us for more help