convert .NET generic List to F# list

12,325

Solution 1

Try List.ofSeq in the Microsoft.FSharp.Collections namespace.

#                     List.ofSeq : seq<'T> -> 'T list

It's not specifically for System.Collections.Generic.List<T>, but for IEnumerable<T> (seq<'T> in F#) types in general, so it should still work.

(It's also not strictly built into the F# language, but neither is List<T> built into C# or VB.NET. Those are all part of the respective standard libraries.)

Solution 2

Given IEnumerable<T> foo you would do the following (in C#) to get an F# list<T>:

 var fsharpList = ListModule.OfSeq(foo);

ListModule refers to Microsoft.FSharp.Collections.ListModule, and is referred to as List from within F# itself.

Share:
12,325
BuddyJoe
Author by

BuddyJoe

I like to code C# and work with the web. Still learning.

Updated on June 06, 2022

Comments

  • BuddyJoe
    BuddyJoe almost 2 years

    Is there a built-in method to convert the .NET List<> into the F# list?

  • BuddyJoe
    BuddyJoe almost 14 years
    I also noticed with the 2.0 compiler type inference the List<Something> on the right hand side shows up as a seq (or IEnumerable) on the left hand side. Pretty smart of those guys (+1)
  • Joel
    Joel about 9 years
    There's no such thing as ListModule in F#
  • basarat
    basarat about 9 years
    Its not specified if the question was about C# or F#. This code is in C#. There is a ListModule in F#. Its just called list in F#, so list.ofSeq in F# == ListModule.OfSeq in C#.