Click here to Skip to main content
15,867,308 members
Home / Discussions / C#
   

C#

 
GeneralRe: Convert code .Net4.0 to code .Net 2.0 Pin
CHill6028-Apr-16 0:54
mveCHill6028-Apr-16 0:54 
Question'out' keyword, to use or not to use Pin
Foothill19-Apr-16 8:35
professionalFoothill19-Apr-16 8:35 
AnswerRe: 'out' keyword, to use or not to use Pin
clapclap19-Apr-16 9:05
clapclap19-Apr-16 9:05 
GeneralRe: 'out' keyword, to use or not to use Pin
Foothill19-Apr-16 9:24
professionalFoothill19-Apr-16 9:24 
GeneralRe: 'out' keyword, to use or not to use Pin
Sascha Lefèvre19-Apr-16 9:29
professionalSascha Lefèvre19-Apr-16 9:29 
GeneralRe: 'out' keyword, to use or not to use Pin
Foothill19-Apr-16 9:51
professionalFoothill19-Apr-16 9:51 
GeneralRe: 'out' keyword, to use or not to use Pin
F-ES Sitecore21-Apr-16 0:26
professionalF-ES Sitecore21-Apr-16 0:26 
AnswerRe: 'out' keyword, to use or not to use Pin
Sander Rossel19-Apr-16 11:08
professionalSander Rossel19-Apr-16 11:08 
I find the argument that devs need skills to use out and ref a bit lame.
We're professional programmers and we write for other professional programmers. I really hope professional programmers grasp the concept of reference and value types or we're going to have bigger problems than out and ref.

That said, I still don't think the use of out and ref is particularly good design.
As a rule a function should do one thing only, that means it probably produces at most one value (in some cases that value is a complex object containing many properties).
Functions that are pure (i.e. have no side-effects) are easier to grasp, change, and maintain as well, using out or ref is a side-effect. Having pure functions is a more functional approach to programming (which we can't or should fully implement in C#, but still has benefits).
In most cases I prefer validating all input thoroughly, if success do the thing else return and show the user the validation results. If the thing still fails there must be something really wrong. Returning a bool won't do, an Exception is the way to go.
Of course the TryParse/TryWhatever and DivRem methods are acceptable exceptions to this rule. Mostly because it's convention, the alternative would be something like "if (int.CanParse(value)) int.Parse(value)".
In the case of DivRem the remainder is used to calculate the quotient and while you have the remainder anyway it would be a waste to throw it away just because returning two values is "bad design".
Personally I've never needed out or ref Smile | :)
Read my (free) ebook Object-Oriented Programming in C# Succinctly.
Visit my blog at Sander's bits - Writing the code you need.
Or read my articles here on CodeProject.
Simplicity is prerequisite for reliability.
— Edsger W. Dijkstra
Regards,
Sander

GeneralRe: 'out' keyword, to use or not to use Pin
Foothill19-Apr-16 11:38
professionalFoothill19-Apr-16 11:38 
GeneralRe: 'out' keyword, to use or not to use Pin
Sander Rossel19-Apr-16 12:24
professionalSander Rossel19-Apr-16 12:24 
GeneralRe: 'out' keyword, to use or not to use Pin
Foothill19-Apr-16 14:06
professionalFoothill19-Apr-16 14:06 
GeneralRe: 'out' keyword, to use or not to use Pin
Sander Rossel19-Apr-16 21:25
professionalSander Rossel19-Apr-16 21:25 
GeneralRe: 'out' keyword, to use or not to use Pin
Foothill20-Apr-16 5:14
professionalFoothill20-Apr-16 5:14 
GeneralRe: 'out' keyword, to use or not to use Pin
Brisingr Aerowing20-Apr-16 4:23
professionalBrisingr Aerowing20-Apr-16 4:23 
GeneralRe: 'out' keyword, to use or not to use Pin
F-ES Sitecore21-Apr-16 0:33
professionalF-ES Sitecore21-Apr-16 0:33 
GeneralRe: 'out' keyword, to use or not to use Pin
Foothill21-Apr-16 4:39
professionalFoothill21-Apr-16 4:39 
GeneralRe: 'out' keyword, to use or not to use Pin
Richard MacCutchan19-Apr-16 20:46
mveRichard MacCutchan19-Apr-16 20:46 
GeneralRe: 'out' keyword, to use or not to use Pin
Sander Rossel19-Apr-16 21:26
professionalSander Rossel19-Apr-16 21:26 
GeneralRe: 'out' keyword, to use or not to use Pin
Mycroft Holmes19-Apr-16 21:54
professionalMycroft Holmes19-Apr-16 21:54 
GeneralRe: 'out' keyword, to use or not to use Pin
Foothill20-Apr-16 5:03
professionalFoothill20-Apr-16 5:03 
GeneralRe: 'out' keyword, to use or not to use Pin
Sander Rossel20-Apr-16 5:59
professionalSander Rossel20-Apr-16 5:59 
AnswerRe: 'out' keyword, to use or not to use Pin
Gerry Schmitz19-Apr-16 12:36
mveGerry Schmitz19-Apr-16 12:36 
AnswerRe: 'out' keyword, to use or not to use Pin
Bernhard Hiller19-Apr-16 21:25
Bernhard Hiller19-Apr-16 21:25 
GeneralRe: 'out' keyword, to use or not to use Pin
Foothill20-Apr-16 3:16
professionalFoothill20-Apr-16 3:16 
AnswerRe: 'out' keyword, to use or not to use Pin
V.19-Apr-16 22:04
professionalV.19-Apr-16 22:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.