简体   繁体   English

在F#中传递`out`参数(作为参考)

[英]Pass `out` parameter (as reference) in F#

I want to call System.Uri.TryCreate() : 我想调用System.Uri.TryCreate()

let uriResult: System.Uri = ref (* what's here? *);
System.Uri.TryCreate(uriName, UriKind.Absolute, uriResult)

As I've learned here , one has to pass a F# reference for .NET out parameters. 正如我在这里学到的,必须为.NET 输出参数传递F#参考。

But how can I initialize my reference uriResult in my case? 但是我如何在我的情况下初始化我的参考uriResult

I've tried creating a new, empty Uri object. 我试过创建一个新的空Uri对象。

let uriResult: System.Uri = ref (new System.Uri());

Error 1 错误1
This expression was expected to have type 该表达式预计具有类型
Uri 乌里
but here has type 但这里有类型
'a ref '参考

As explained in Parameters and Arguments on MSDN (see Passing by Reference), out parameters are automatically tuplized, so you can do this: 如MSDN上的参数和参数中所述(请参阅按引用传递),out参数会自动进行tuplized,因此您可以执行以下操作:

match System.Uri.TryCreate(uriName, UriKind.Absolute) with
| true, uriResult -> //OK
| _ -> //not a Uri

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM