eMko
eMko

Reputation: 1143

F# list to C# IEnumerable: most efficient method?

I'm currently working on an F# library with GUI written in C# and I would like to ask what is the best or correct way to pass an F# (generic) list to a C# code (generic IEnumerable).

I've found three ways so far:

[1; 2; 3; 4; 5;] |> List.toSeq

[1; 2; 3; 4; 5;] |> Seq.ofList 

[1; 2; 3; 4; 5;] :> seq<int>

Is there any practical difference between these three methods, please?

Upvotes: 13

Views: 2418

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243041

If you look in the F# library source code, you'll find out that they are all the same:

In terms of readability, I would probably use Seq.ofList or List.toSeq, especially if the code is a part of a larger F# pipeline, because then it makes the code a bit nicer:

someInput
|> List.map (fun x -> whatever) 
|> List.toSeq

Upvotes: 17

Related Questions