-11
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); 
}

compile error:

Error   1   The best overloaded method match for 'ConsoleApplication1.func1.Class1.test(out int)' has some invalid arguments
abelenky
  • 63,815
  • 23
  • 109
  • 159
lovespring
  • 19,051
  • 42
  • 103
  • 153
  • @mclaassen: Says who? It's no different from `int a = 1; a = 5;` Perfectly legal to write to a variable more than once. – Ben Voigt Aug 13 '14 at 18:22
  • possible duplicate of [What is the difference between ref and out? (C#)](http://stackoverflow.com/questions/516882/what-is-the-difference-between-ref-and-out-c) – Dave Zych Aug 13 '14 at 18:23
  • @BenVoigt Ya nevermind, what I really had in my head that you must assign it a value in the function. – mclaassen Aug 13 '14 at 18:23
  • Can you please clarify what additional information about [out (C# Reference)](http://msdn.microsoft.com/en-us/library/t3c3bfhx.aspx) you need? – Alexei Levenkov Aug 13 '14 at 18:24
  • 3
    Next time please **read the documentation**. – tnw Aug 13 '14 at 18:26

2 Answers2

4

I fixed it for you:

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

When calling a function that has an out or ref parameter, you need to pay some respect to that, by marking your parameter as out or ref as well.

MSDN Reference Page

To use an out parameter, both the method definition and the calling method must explicitly use the out keyword.

abelenky
  • 63,815
  • 23
  • 109
  • 159
3

You need the outkeyword in the call:

c1.Test(out a); 

Except of that the captialization of the name is wrong (test and Test)

Flat Eric
  • 7,971
  • 9
  • 36
  • 45
  • I see! it's a strange syntax, unlike the c++ reference. – lovespring Aug 13 '14 at 18:23
  • @lovespring: It is akin to using the address-of operator (`&`) in C or C++, when the function takes its buffer by pointer. (And in MSIL, calling a function with an `out` or `ref` parameter does actually need an address-of instruction) – Ben Voigt Aug 13 '14 at 18:25