Take the 2-minute tour ×

I know that the f# list is not the same at the c# List. What do I need to do to be able to pass a list of ints from a c# application to an f# library? I'd like to be able to use pattern matching on the data once it's in the f# code.

share|improve this question

4 Answers 4

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 the mutable list, you don't need to do anything special, but you won't be able to use pattern matching. Or, rather, you can define active patterns for System.Collections.Generic.List<'a>; it's just a bad idea.

share|improve this answer
Just wondering, but why wouldn't a person want to write a few active patterns that operate on C# lists? –  Juliet Dec 24 '08 at 18:41

You can pass a sequence of ints - it's basically anything that supports IEnumerable<int>.

share|improve this answer
up vote 1 down vote accepted

Here is how I ended up doing it.

The 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

The c Sharp code:

    List<int> myInts = new List<int> { 5, 6, 7 };
    int max = FSModule.FindMax(myInts);
share|improve this answer

You can reference C# Assemblies from F# projects. Expose your list via a referenced assembly.

share|improve this answer

Your Answer


