Delegate interoperability between F#, C# and Visual Basic

Article ID: 2843733 - View products that this article applies to.

About Author:

Collapse this tableExpand this table
Collapse this imageExpand this image
2401266
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.

Expand all | Collapse all

On This Page

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.

For example:

        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):

        Process[] processList = Process.GetProcesses()
        IEnumerable<string> strs = SeqModule.Map<Process, string>(processNamesu004025, (IEnumerable<Process>)processList);
      

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#:

public static IEnumerable<TResult> Map<T, TResult>(Microsoft.FSharp.Core.FSharpFunc<T,TResult> mapping, System.Collections.Generic.IEnumerable<T> source)
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:

ProcessNamesu40025 processNamesu004025;

Actual implementation of the processNamesu004025 is actually a class itself that derives from FSharpFunc:

        [Serializable]
        internal class ProcessNamesu004025 : FSharpFunc<Process,String>
        {
        ProcessNamesu004025() { }

        public override String Invoke(Process p)
        {
        return p.ProcessName;
        }
        }
      
This is where the semantic really differs.

Using C#/Visual Basic delegate in F#

Using C#/Visual Basic delegate in F# is simple, with the help of Microsoft F# Powerpack.

This useful library can be downloaded from:

http://fsharppowerpack.codeplex.com/

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:

        public static FSharpFunc<T1, FSharpFunc<T2, FSharpFunc<T3, FSharpFunc<T4, Unit>>>> ToFSharpFunc<T1, T2, T3, T4>(Action<T1, T2, T3, T4> f);
        public static FSharpFunc<T1, FSharpFunc<T2, FSharpFunc<T3, Unit>>> ToFSharpFunc<T1, T2, T3>(Action<T1, T2, T3> f);
        public static FSharpFunc<T1, FSharpFunc<T2, Unit>> ToFSharpFunc<T1, T2>(Action<T1, T2> f);
        public static FSharpFunc<T1, FSharpFunc<T2, FSharpFunc<T3, FSharpFunc<T4, U>>>> ToFSharpFunc<T1, T2, T3, T4, U>(Func<T1, T2, T3, T4, U> f);
        public static FSharpFunc<T1, FSharpFunc<T2, FSharpFunc<T3, U>>> ToFSharpFunc<T1, T2, T3, U>(Func<T1, T2, T3, U> f);
        public static FSharpFunc<T1, FSharpFunc<T2, U>> ToFSharpFunc<T1, T2, U>(Func<T1, T2, U> f);
      
This is the sample case:

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.

More information

For more information, please visit:


Community Solutions Content Disclaimer

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.

Properties

Article ID: 2843733 - Last Review: May 7, 2013 - Revision: 2.0
Applies to
  • 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
Keywords: 
KB2843733

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com