is it possible to use out and ref keyword together???

  • Thread starter Thread starter selva
  • Start date Start date
S

selva

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
 
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
 
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
 
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
 
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
 
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
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.<
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top