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?
If you look in the F# library source code, you'll find out that they are all the same:
Seq.ofList
just calls List.ofSeq
as you can see here in the "list.fs" file
List.toSeq
is implemented using s :> seq<_>
as you can see here in the "seq.fs" file
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With