简体   繁体   English

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

[英]How can you use C# parameter modifiers from Haxe?

I'm currently porting my C# code to Haxe, but am having difficulty discerning how to use C# parameter modifiers such as Out or Ref from Haxe 我目前正在将C#代码移植到Haxe,但是很难辨别如何使用Haxe的C#参数修饰符,例如Out或Ref。

For example, the Out parameter is used in functions like TryGetValue in many of the C# Generic collections. 例如,在许多C#Generic集合中的TryGetValue之类的函数中都使用了Out参数。 In the example below using Haxe, it passes Haxe compilation but complains when compiling with the Mono compiler that an Out modifier is required, which is what I had thought it was doing. 在下面使用Haxe的示例中,它通过Haxe编译,但是在使用Mono编译器进行编译时抱怨需要使用Out修饰符,这就是我认为的做法。

Example Code 范例程式码

// 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
}

Links to the Parameter Typedefs in Haxe for Parameter Modifiers in C# 链接到Haxe中用于C#中参数修改器的参数Typedef

Is there anyone who can show me the proper way to use the Haxe typedefs for parameter modifiers in Haxe? 有谁可以向我展示在Haxe中使用Haxe typedef作为参数修饰符的正确方法吗? Thank you. 谢谢。

As noted in the doc, the Out type is meant to be used in function parameters only. 如文档中所述, Out类型只能在函数参数中使用。 So basically you only need to type your variable normally. 因此,基本上,您只需要正常键入变量即可。

The following code works for me: 以下代码对我有用:

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() {}
}

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

相关问题 如何在C#正则表达式中使用内联修饰符? - How to use inline modifiers in C# regex? C#重载的构造函数仅在使用参数修饰符时不能有所不同 - C# Overloaded constructor cannot differ on use of parameter modifiers only 如何使用纯 C# 中的 ObjectAnimationUsingKeyFrames? - How can you use ObjectAnimationUsingKeyFrames from pure C#? Haxe 编译器可以将任何 C# 代码转换为 Haxe 吗? - Can Haxe compiler convert any C# code to Haxe? C#:可以将布尔谓词用作if语句的参数吗? - c#: can you use a boolean predicate as the parameter to an if statement? C# SQLite:可以用参数代替表名吗? - C# SQLite: Can you use a parameter in place of a table name? 在 c# 中声明一个类时可以拥有的最大有效修饰符数量是多少? - What is max number of valid modifiers you can have while declaring a class in c#? C#:使用getter / setter shortand时,可以混合使用函数访问修饰符吗? - C#: Can you mix function access modifiers when using getter/setter shortand? 如何使用字符串实例化对象并在C#中发送参数? - How can you instantiate an object with a string and send a parameter in C#? C#:在文本框的 keydown 事件中,如何检测当前按下的修饰符 + 键? - C#: In the keydown event of a textbox, how do you detect currently pressed modifiers + keys?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM