How to not pass an "out" parameter?

  • Thread starter Thread starter None
  • Start date Start date
N

None

If a method takes an "out int" as a parameter is it possible to not pass
anything to that parameter? "null" and "out null" does not work.
 
None said:
If a method takes an "out int" as a parameter is it possible to not pass
anything to that parameter? "null" and "out null" does not work.

It would be handy if C# had something like the "anonymous variable" in
Prolog, which means, in essence, "allow a value to be placed here, but then
ignore it."
 
None said:
If a method takes an "out int" as a parameter is it possible to not pass
anything to that parameter? "null" and "out null" does not work.

The semantics of the out keyword on a method parameter means that the
method will store some result in the parameter. The compiler checks and
enforces that all code paths will store some result in the parameter
before the method returns.

Obviously, something must be passed in (though it need not be
initialized), or it will not be possible for the method to fulfill its
contract.
 
out as a parameter is a contract; the method is obliged to fill it in. I'd
say you have three options:

1) The out really is a contract; therefore if you can't fulfill that you
throw an exception.
2) The value coming out is optional, in which case change it to a ref int,
and indicate in some other way whether you actually filled in the value.
3) The out is more like a db integer; you want to be able return null. You
might want to consider looking at the SqlTypes in the System.Data namespace.

Hope that helps.
 
Back
Top