从C#代码内部访问F#列表 [英] Accessing an F# list from inside C# code

查看:63
本文介绍了从C#代码内部访问F#列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个F#模块,里面有一个列表:

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

module MyModule
type X = 
    {
        valuex : float32
    }
let l = [ for i in 1 .. 10 -> {valuex =  3.3f}]

现在从C#类尝试访问先前定义的列表,但是我不知道如何转换它:

Now from a C# class I'm trying to access the previously defined list, but I don't know how converting it:

... list = MyModule.l ; //here's my problem

我需要类似的东西:

IList<X> list = MyModule.l;

我该如何实现?

推荐答案

很简单:

IList<MyModule.X> list = MyModule.l.ToList();

之所以需要转换方法而不是强制转换/隐式转换,是因为FSharpList<T>实现了IEnumerable<T>而不是IList<T>,因为它表示一个不变的链表.

The reason you need the conversion method rather than a cast / implicit conversion is because an FSharpList<T> implements IEnumerable<T> but not IList<T> since it represents an immutable linked-list.

请注意,您必须在C#项目中包含FSharp.Core作为参考.

Note that you'll have to include FSharp.Core as a reference in your C# project.

这篇关于从C#代码内部访问F#列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆