Accessing an F# list from inside C# code

前端 未结 2 1124
囚心锁ツ
囚心锁ツ 2021-01-12 22:20

I have written an F# module that has a list inside:

module MyModule
type X = 
    {
        valuex : float32
    }
let         


        
2条回答
  •  無奈伤痛
    2021-01-12 23:05

    The FSharpList (which is the .Net name of the F# list type) doesn't implement IList, because it doesn't make sense.

    IList is for accessing and modifying collections that can be accessed by index, which list is not. To use it from C#, you can either use the type explicitly:

    FSharpList l = MyModule.l;
    var l = MyModule.l; // the same as above
    

    Or you can use the fact that it implements IEnumerable:

    IEnumerable l = MyModule.l;
    

    Or, if you do need IList, you can use LINQ's ToList(), as Ani suggested:

    IList l = MyModule.l.ToList();
    

    But you have to remember that F# list is immutable and so there is no way to modify it.

提交回复
热议问题