best way to use: an out method parameter or a method with return v

  • Thread starter Thread starter lianqtlit
  • Start date Start date
If you only have one return value, creating an out parameter will force the
caller to define the parameter before calling the method. Another issue to
remember is using the return value is optional so you can call the method
without declaring any object for the return value.

In my opinion out parameters are handy when you need to return two or more
values, but should not be used of there is only one return value.
 
Have a look at Int.Parse & Int.TryParse to see an example of return
value & out value respectively.
Try to understand scenarios in which you would use either of it, as
Morten described it above.

Kalpesh
 
If you are talking about a single value, then a "return" is absolutely
the correct way to go. Apart from any other considerations, that
expresses what we are trying to do.

"out" and "ref" parameters are powerful, but can cause confusion. I'm
not saying avoid them completely, but when you do use them it is worth
ensuring that it is for the right reasons. The most common "out" usage
recently is probably in the TryParse, TryGetValue, etc signatures,
where it needs to return a bool (success) and the value itself. Using
a return struct would have made it harder to use.

Jon has a good write-up on parameter passing (inc. out):
http://www.yoda.arachsys.com/csharp/parameters.html

Aside: from this and your earlier post, you seem to be trying to
micro-optimise. I'm guessing a C++ background?
Forget it; that is the job of the compiler and (more often) the JIT;
concentrate on making your code expressive and clear (and correct). If
performance is a major concern then use a profiler and spend time
looking at the stuff that is actually, demonstrably and testably
hurting performance.

Marc
 
No I don't have C++ background
what is a profiler?
what do you ment by code expressive?
 
thanks morten

Morten Wennevik said:
If you only have one return value, creating an out parameter will force the
caller to define the parameter before calling the method. Another issue to
remember is using the return value is optional so you can call the method
without declaring any object for the return value.

In my opinion out parameters are handy when you need to return two or more
values, but should not be used of there is only one return value.

--
Happy Coding!
Morten Wennevik [C# MVP]


lianqtlit said:
best way to use: an out method parameter or a method with return ?
 
No I don't have C++ background
what is a profiler?

A tool to show you which parts of your code are taking a lot of time/
memory to execute.
what do you ment by code expressive?

Code is expressive if it clearly expresses the intentions of its
author.

Jon
 
Jon answered the questions ("expressive" and "profiler") - but it also
sounds like I got the wrong idea about /why/ you were asking - so my
apologies for that.

I hope that we (collectively) have helped with your questions; keep
'em coming... ;-p

Marc
 
In performance wise when both have one return value( out int n & return (int)
n)
which is more costly to use?

Morten Wennevik said:
If you only have one return value, creating an out parameter will force the
caller to define the parameter before calling the method. Another issue to
remember is using the return value is optional so you can call the method
without declaring any object for the return value.

In my opinion out parameters are handy when you need to return two or more
values, but should not be used of there is only one return value.

--
Happy Coding!
Morten Wennevik [C# MVP]


lianqtlit said:
best way to use: an out method parameter or a method with return ?
 
which is more costly to use?

Ah! There's that micro-optimisation I was talking about earlier ;-p
The one that makes the code harder to read.

They are pretty much the same thing at the bytecode level (the order
on the stack might be different). It would be rare that this is a
serious consideration; so if you are returning a single value, use
"return".

Marc
 
In performance wise when both have one return value( out int n & return (int)
n)
which is more costly to use?

The first is more costly - in terms of thinking time and maintenance.

I'd be very surprised if you ever encounter the situation where the
execution time efficiency is significant, or where you wouldn't be
better putting optimisation effort in elsewhere first.

Jon
 

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