Groups | Blog | Home
all groups > c# > november 2006 >

c# : is it possible to use out and ref keyword together???


selva
11/23/2006 10:23:48 PM
hi,
currently iam working on c#
i need to pass a parameter inside a function which is a OUT parameter
and i need to pass it as a reference how to do this?
please help me regarding this issue........

regards
bharathi
Duggi
11/23/2006 10:51:16 PM
Hi Selva,
OUT parameter works exactly as reference parameter. The only difference
is that you do not need to initialize the OUT variable before passing
the variable to the method.

if you want to force the method user to pass some data to the method,
you can use ref, if not use OUT. In both the cases data changes to the
param would reflect back to the calling method.

Thanks
-Cnu



[quoted text, click to view]
Jeff Louie
11/23/2006 11:13:28 PM
You might also wish to consider an alternative design to using OUT or
ref and
instead return single "values" or return an immutable structure if you
need to
return more than one "value".

Regards,
Jeff

Kevien Lee
11/24/2006 1:37:57 AM

if you are use a ref type ,you didn't need to use the ref/out :)

[quoted text, click to view]
Truong Hong Thi
11/24/2006 2:42:31 AM

Does anyone know why C# does not support "const" keyword on parameter
to avoid reassign the reference? That keyword makes me feel much more
confident when passing reference-type argument.

It is so rare to see reference-type passed by ref (except for
developers who just switched from some other language). But "out", I
think, is OK. It indicates that the author just wants something out,
and don't need to instantiate the variable before passing (null is OK).
Without "out", it suggests that the author wants to gather some
additional info into and existing variable.

Thi
http://thith.blogspot.com

[quoted text, click to view]
Marc Gravell
11/24/2006 9:46:54 AM
In 99.9% of cases. There are (rare) occasions when you genuinely intend to
reassign a reference that was passed in, and you can't (for some reason)
return the new reference in the return value.

But yes; in many cases "ref" on a reference type parameter is due to
misconceptions. In contrast, "out" is IMO becoming more mainstream and more
useful that "ref", largely due to adopting the TryGetValue and TryParse
signatures from the 2.0 classes.

Marc

Jeff Louie
11/25/2006 4:21:22 PM
Marc... I agree that using a single out parameter is useful as in
TryParse, but a more object oriented approach is simpler and less error
prone for multiple parameters. For instance:

public Mortgage.Calculation Calculate(Mortgage.Parameters m)
{
return Calculate(m.Principal, m.Interest, m.Months, m.Payment,
m.Target);
}

Is easier to use and less error prone than:

private bool TryCalculate(Mortgage.Parameters p,out double
principal,out double interest,out int months,out double payment,out int
target)
{
Mortgage.Calculation result= this.Calculate(p.Principal,
p.Interest, p.Months, p.Payment, p.Target);
principal = result.Principal;
interest = result.Interest;
months = result.Months;
payment = result.Payment;
target = result.Target;

return result.IsValid();
}

Regards,
Jeff
[quoted text, click to view]
misconceptions. In contrast, "out" is IMO becoming more mainstream and
more useful that "ref", largely due to adopting the TryGetValue and
TryParse signatures from the 2.0 classes.<


AddThis Social Bookmark Button