out parameter ???

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Hi,

This what the documentation says about out-parameters
The value of an out argument will not be passed to the out parameter.

But ...

static void F(out int i)
{
i = 9; ==> set breakpoint here
Console.WriteLine(i);
}
static void Main()
{
int i = 10;
F(out i);
}

I set a breakpoint in F() and see that 'i' has a value of 10 before it is
being assigned the value 9. Agree, you can't really use the value of 10 but
apparently the value is sent over the wire if F() would be implemented on a
server somewhere.

Is the doc wrong or what is happening here ?

thnx
Chris
 
Hi Chris,

What documents would that be?
The framework SDK says a variable passed as an 'out' argument "need not"
be initialized. There is nothing about initialized variable values not
being sent. The only requirement is that you initialize it inside the
method. 'ref' arguments differ from 'out' arguments only in that they
need to be initialized before the method and do not need to be initialized
inside the method.

Happy coding!
Morten
 
Hi. This is what O'reilly's C# language reference says:
"While a ref modifier requires that a variable be assigned a value before
being passed to a method, the out modifier requires that a variable be
assigned a value before returning from a method."

In your case, the i variable is already assigned with a value, so it will
return from method F without any problem as it already has a value. I have
not tried but I think that a real problem would be if you pass i without a
value and inside the method you don't assign a value to i, the compiler
should rise an error.

regards, alex.
 
Hi,

what document ? just the MSDN-help.
- press F1 on 'out 'in VisualStudio
- or
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfout.asp

3rd paragraph.

I know that the value doesn't need not be initialized before calling the
function but yes, needs to be assigned a value in the function before
leaving the function.
But the fact that the value is passed to the function as well with 'out'
parameters confuses me a little.

In IDL (interface definition language) there exists a difference between
'in', 'in-out' and 'out'-parameters.

The equivalent in a C# would be :

in (MIDL) C# equivalent : call by value
in,out (MIDL) C# equivalent : call using 'ref'
out (MIDL) C# equivalent : call using 'out'

The purpose of the out-parameter is very useful if you want to get a large
of amount of data OUT of the server only. So no data is sent accros the wire
TO the server (in), when invoking the function, only FROM the server (out)
to the client

I expect the same behavior here (as the MSDN help says it), but no, it
behaves just like a 'ref'-parameter (so as a 'in-out' parameter) apart from
the initilization stuff.

Am I right to be confused or am I overlooking something ?

thnx
Chris
 
I expect the same behavior here (as the MSDN help says it), but no, it
behaves just like a 'ref'-parameter (so as a 'in-out' parameter) apart from
the initilization stuff.

Am I right to be confused or am I overlooking something ?

You're overlooking the fact that what happens when using remoting will
almost certainly not be the same as what happens when using normal
execution.

A convenient way of achieving "out" semantics in normal execution is to
just pass the address of the variable. That means that in the debugger,
you will be able to see the previous value, even though the variable
isn't actually definitely assigned yet.

That doesn't mean that the previous value (if any) will be passed
between servers in a remoting situation. Remoting clearly requires
extra intervention on the part of the CLR anyway, in order to get the
values back and forth - so it's not much of a stretch to believe that
it will do the right thing, and only send back the parameter.

The fact that you can see the previous value in the debugger is
basically just a coincidence - an aberration which should only be
observable in a debugger, and shouldn't be relied on or extrapolate
remoting behaviour from.
 
Back
Top