从传递到C#的F#列表检索项 [英] Retrieving items from an F# List passed to C#

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

问题描述

我有被称为在F#在C#中的函数,传递它的参数 Microsoft.FSharp.Collections.List<对象>

I have a function in C# that is being called in F#, passing its parameters in a Microsoft.FSharp.Collections.List<object>.

我如何能够获得在C#函数从F#列表中的项目?

How am I able to get the items from the F# List in the C# function?

修改

我已经找到了功能性风​​格的方式,通过他们循环,并可以将它们传递给函数如下返回C#System.Collection.List:

I have found a 'functional' style way to loop through them, and can pass them to a function as below to return C# System.Collection.List:

private static List<object> GetParams(Microsoft.FSharp.Collections.List<object> inparams)
{
    List<object> parameters = new List<object>();
    while (inparams != null)
    {
        parameters.Add(inparams.Head);
        inparams = inparams.Tail;
     }
     return inparams;
 }

再次编辑

的F#列表,正如指出的下面,是可枚举,因此,上述功能可以被替换的行;

The F# List, as was pointed out below, is Enumerable, so the above function can be replaced with the line;

new List<LiteralType>(parameters);

有什么办法,但是,通过索引引用一个项目的F#列表?

Is there any way, however, to reference an item in the F# list by index?

推荐答案

在一般情况下,避免暴露F#的特异类型(如F#'名单'型),以其他语言,因为经验是不是所有的伟大的(如你可以看到)。

In general, avoid exposing F#-specific types (like the F# 'list' type) to other languages, because the experience is not all that great (as you can see).

这是F#列表是一个IEnumerable,所以你可以创建如从这种方式pretty的动辄System.Collections.Generic.List。

An F# list is an IEnumerable, so you can create e.g. a System.Collections.Generic.List from it that way pretty easily.

目前没有有效的索引,因为它是一个单链接列表等访问任意元素为O(n)。如果你想要那个索引,改变为另一种数据结构是最好的。

There is no efficient indexing, as it's a singly-linked-list and so accessing an arbitrary element is O(n). If you do want that indexing, changing to another data structure is best.

这篇关于从传递到C#的F#列表检索项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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