c# - 如何使用 Haxe 的 C# 参数修饰符?

标签 c# parameters haxe

我目前正在将我的 C# 代码移植到 Haxe,但我很难辨别如何使用来自 Haxe 的 C# 参数修饰符,例如 Out 或 Ref

例如,Out 参数用于许多 C# 通用集合中的 TryGetValue 等函数。在下面使用 Haxe 的示例中,它通过了 Haxe 编译,但在使用 Mono 编译器编译时提示需要 Out 修饰符,这正是我所认为的。

示例代码

// In C#, looks like this..`.
KeyValuePair<T,V>? element = null;
if (!dictionary.TryGetValue(key, out element))
{
     // Do foo
}

// In Haxe?
var element:Out<KeyValuePair<T,V>> = null;
if (!dictionary.TryGetValue(key, element))
{
     // Do foo
}

C# 参数修饰符的 Haxe 参数类型定义链接

有没有人可以告诉我在 Haxe 中使用 Haxe typedef 作为参数修饰符的正确方法?谢谢。

最佳答案

如文档中所述,Out 类型仅用于函数参数。所以基本上你只需要正常输入你的变量。

以下代码适用于我:

import cs.system.collections.generic.Dictionary_2;

class Main {
    static function main() {
        var dictionary = new Dictionary_2<String, Foo>();
        dictionary.Add('haxe', new Foo());

        // type inference works
        var implicit = null;
        dictionary.TryGetValue('haxe', implicit);
        trace(implicit);

        // this also works
        var explicit:Foo = null;
        dictionary.TryGetValue('haxe', explicit);
        trace(explicit);
    }
}

class Foo {
    public function new() {}
}

关于c# - 如何使用 Haxe 的 C# 参数修饰符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29897319/

相关文章:

ruby-on-rails - 在Rails中,如何转换参数[:id] to Integer

c# - JQuery Carosellite 和来自 c# 的循环和参数

javascript - 是否可以在 Haxe 中重写 js.html.Window 类的函数?

terminal - haxe cpp - 如何在终端应用程序中捕获 Ctrl+C 按键?

c# - 为什么 .NET 中的 ceiling 返回 Double 而不是整数?

c# - 动态字符串/List<string> 参数的最佳实践

c# - DeviceTwin 更新属性时出现问题

c# - mysql 中可选的 where 子句条件

c++ - 你能在不使用数组的情况下将不同数量的相同类型的参数传递给函数吗?

c++ - HaxePunk:导出到 C++ 时没有渲染