在C#列表和F#列表之间转换 [英] Converting between C# List and F# List
问题描述
备注:这是一份自我说明,但是如果您有任何其他建议,或者如果我发现任何明显的错误/遗漏,我将非常感谢您的帮助.
Remark: This is a self-documentation, but if you have any other suggestions, or if I made any mistakes/miss out something obvious, I would really appreciate your help.
来源:
我是F#和C#的初学者,我想将C#列表转换为F#列表,反之亦然.这样做的代码/命令会根据您的代码位置(在C#或F#中)而变化.
I am a beginner in F# and C#, and I want to convert a C# List to an F# List, and vice versa. The code/commands for doing so changes depending on where is your code (in C# or F#).
请在下面查看我的答案.如果您还有其他建议,请告诉我.谢谢.
Please see my answer below. If you have any other suggestions, please let me know. Thank you.
推荐答案
在C#文档中,我可以使用ListModule.OfSeq
和.ToList()
,如下所示:
Inside a C# document, I can use ListModule.OfSeq
and .ToList()
, like this:
// Currently inside a C# document
var CS_list = new List<int> {1,2,3};
var FS_list = ListModule.OfSeq(CS_List); // Microsoft.FSharp.Collections.ListModule
var CS_converted_back = FS_List.ToList();
在F#文档中,我可以使用Seq.toList
和ResizeArray<_>
,如下所示:
Inside an F# document, I can use Seq.toList
and ResizeArray<_>
, like this:
// Currently inside an F# document
let FS_list = [1;2;3]
let CS_list = ResizeArray<int> FS_list // System.Collections.Generic.List
let FS_converted_back = Seq.toList CS_list
如果我犯了任何错误,或者我错过了明显的事情,请告诉我.
If I made any mistakes, or if I miss out something obvious, please let me know.
这篇关于在C#列表和F#列表之间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!