This article is provided by MVP Eriawan Kusumawardhono. Microsoft is so thankful that MVPs who proactively share their professional experience with other users. The article would be posted on MVP's website or blog later.
Overview of delegates in F# and C#/Visual Basic
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:
var procnames = Process.GetProcesses().Select(p => p.ProcessName);
The Visual Basic sample is:
Dim procnames = Process.GetProcesses().Select(Function(p) p.ProcessName)
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.
open System.Diagnostics let ProcessList = Process.GetProcesses() let ProcessNames = ProcessList |> Seq.map(fun p -> p.ProcessName)
In the code above, the "fun p-> p.ProcessName" has different meaning because the F# compiler will translate this line:
let ProcessNames = ProcessList |> Seq.map(fun p -> p.ProcessName)
into this (in C# using decompiler and simplified):
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#:
public static FSharpFunc<T1, U> ToFSharpFunc<T1, U>(Func<T1, U> f);
There are also overrides of ToFSharpFunc to use when dealing with other types of Func:
The F# code below will return the index of the first found data:
static member GetRowIndex (datasource: IEnumerable<'t>, functionToFind: 't -> bool) : int = let paramcheck = datasource <> null match paramcheck with | true -> Seq.findIndex functionToFind datasource | _ -> -1
To use this function of GetRowIndex in C#/Visual Basic, we simply call ToFSharpFunc. This is the code in C#:
var proclist = Process.GetProcesses(); int index_devenv = Enumerable4.GetRowIndex(proclist, FuncConvertExtensions.ToFSharpFunc((Process p) => p.WorkingSet64 > 80000000));
Using System.Func delegates in F#
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)
open System open System.Linq open System.Diagnostics let procresult = Process.GetProcesses().Select(new Func<Process,String>(fun p -> p.ProcessName)) let printproc = procresult |> Seq.iter (fun a -> Console.WriteLine(a))
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.
MICROSOFT 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.
Microsoft Visual Studio Express 2012 for Web, Microsoft Visual Studio Premium 2012, Microsoft Visual Studio Professional 2012, Microsoft Visual Studio Test Professional 2012, Microsoft Visual Studio Ultimate 2012