When consuming C# libraries in F# projects, out parameters will be interpreted in F# with type byref. For example:

public class Arithmetic
{
public static void sumAndDifference(
int a, int b, out int sum, out int difference)
{
sum = a + b;
difference = a - b;
}
}

Here, sumAndDifference has the following signature when the library is referenced from an F# project:

The literal way to call the method is as follows, declaring reference value up-front and de-referencing them for use after the method call:

let sum : int ref = ref 1
let difference : int ref = ref 2
Arithmetic.sumAndDifference(5, 3, sum, difference )
printfn "Sum = %d, Difference = %d" !sum !difference

But F# also provides a much more concise syntax, allowing the out parameters to appear on the left hand side of an assignment:

let sum, difference = Arithmetic.sumAndDifference(5, 3)
printfn "Sum = %d, Difference = %d" sum difference

This syntax is so much cleaner and avoids any mention of references.

### Like this:

Like Loading...

*Related*

Filed under F#, Programming

Tagged as F#

Pingback: How to extend F# discriminated unions for use from C# | musingstudio