【问题标题】:About the "out" prefix of function param in C#关于C#中函数参数的“out”前缀
【发布时间】:2014-08-14 02:26:34
【问题描述】:
namespace ...
{
  class Class1
   {
    public void test(out int a){
        a = 1;
        return;
    }

}

}

static void Main()
{
    Class1 c1 = new Class1();
    int a=1;
    c1.Test(a); 
}

编译错误:

Error   1   The best overloaded method match for 'ConsoleApplication1.func1.Class1.test(out int)' has some invalid arguments

【问题讨论】:

  • @mclaassen:说谁?与int a = 1; a = 5; 没有什么不同,多次写入一个变量是完全合法的。
  • @BenVoigt 没关系,我真正想到的是,你必须在函数中为其分配一个值。
  • 您能否说明您需要哪些有关out (C# Reference) 的其他信息?
  • 下次请阅读文档

标签: c# .net


【解决方案1】:

我帮你修好了:

static void Main()
{
    Class1 c1 = new Class1();
    int a=1;
    c1.test(out a); 
}

当调用具有outref 参数的函数时,您需要对此表示尊重,将您的参数也标记为outref

MSDN Reference Page

要使用 out 参数,方法定义和调用方法都必须显式使用 out 关键字

【讨论】:

    【解决方案2】:

    您需要在通话中使用out关键字:

    c1.Test(out a); 
    

    除了名称的大写错误(testTest

    【讨论】:

    • 我明白了!这是一个奇怪的语法,不像 c++ 参考。
    • @lovespring:当函数通过指针获取缓冲区时,它类似于在 C 或 C++ 中使用地址运算符 (&)。 (而在 MSIL 中,调用带有 outref 参数的函数实际上需要地址指令)
    猜你喜欢
    • 2014-08-13
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 2020-02-12
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    相关资源
    最近更新 更多