What can I do to pass a list from C # to F #?

I know that the f # list does not match the C # list. What do I need to do to pass an int list from a C # application to the f # library? I would like to be able to use pattern matching by data when it is in f # code.

+7
c # f #
source share
4 answers

Here is how I did it.

FSharp Code:

let rec FindMaxInList list = match list with | [x] -> x | h::t -> max h (FindMaxInList t) | [] -> failwith "empty list" let rec FindMax ( array : ResizeArray<int>) = let list = List.ofSeq(array) FindMaxInList list 

Sharp Code:

  List<int> myInts = new List<int> { 5, 6, 7 }; int max = FSModule.FindMax(myInts); 
+3
source share

you can use

 Seq.toList : IEnumerable<'a> -> list<'a> 

to convert any IEnumerable<'a> seq to an F # list. Note that F # lists are immutable; if you want to work with a mutable list, you don’t have to do anything, but you won’t be able to use pattern matching. Or rather, you can define active templates for System.Collections.Generic.List<'a> ; it's just a bad idea.

+8
source share

You can pass a sequence of ints - this is basically everything that supports IEnumerable<int> .

+1
source share

You can reference C # Assemblies from F # projects. Run the list through a reference assembly.

0
source share

All Articles