Argument Modifiers: 'ref', 'out', 'params' and 'this'
secretGeek .:dot Nuts about dot Net:.
home .: about .: sign up .: sitemap .: secretGeek RSS

Argument Modifiers: 'ref', 'out', 'params' and 'this'

Some c-sharp devs must slink through their whole skanky career, utterly ignorant about argument modifiers.

There's usually a way to avoid thinking about them: if you're willing to write a lot more code and have your existing stuff crash and get patched many more times.

The best known 'argument modifier' is probably 'ref' -- and it's purpose is pretty damn powerful. I assume you know its purpose: but just imagine you've just gone from a state of not knowing what the ref param modifier does, to a state where you know what it does. Think of all the things you couldn't do before that are now open to you. Hold that thought.

I'd say that I use 'out' possibly a little more often than I use 'ref' -- yet I wouldn't rank it quite so high on the 'mind-blowing' stakes. The 'params' argument modifier is certainly mind-blowing, if you've never used it before. It gives the language a whole big chunk of 'affordance' -- allow you to achieve, very simply, ranges of motion that are otherwise almost inachievable.

But what about the 'this' modifier.

I'd wager that 70% of the C sharp devs out there haven't yet parsed the 'this' argument modifier.

And when they do -- i hope to see the nightsky light up with the colourful spectacle of a hundred thousand minds noisily exploding under the power of extension methods.

If you're part of that sad 70%, go now and learn how to implement an extension method or ten.





'Russell Mull' on Sun, 12 Aug 2007 03:56:49 GMT, sez:

'params' is indeed woefully underused, though I'm not sure why. I see 'ref' sometimes, often used where 'out' should have been. And you can't really blame people for missing out out 'this', as that version of the language isn't exactly *released* yet...



'Zooba' on Sun, 12 Aug 2007 06:58:32 GMT, sez:

The 'this' modifier is mindblowing. 'params' is not. It's been around since old-style VB in an almost identical form and the concept for even longer. The reason the C calling convention (parameters pushed onto the stack in reverse order, caller cleans up) was invented was to support this. It really is nothing special.

I also like 'ref' and 'out', especially the distinction between the two, though it would be nice if there were a way to pass a null reference (though then we're back where we started with pointer parameters... not that I have a problem with that either).



'Buckley' on Sun, 12 Aug 2007 12:03:15 GMT, sez:

Erm, So what is so special about the keywword "this".

I suppose you aren't talking about this in the way that it reduces the scope to the object you are programming in cause the feature hasn't been releases I read in the comments.

Can you clarify this?



'Thomas Eyde' on Sun, 12 Aug 2007 12:06:52 GMT, sez:

I tend to avoid out/ref, as I feel a method's results should be returned as an assignment, not via its parameters.

A lot of out/ref parameters just confuse me. Add a return value to the mix, and I usually fail to understand what the method is supposed to do.



'Adel' on Sun, 12 Aug 2007 16:01:49 GMT, sez:

I know that you are talking mainly about "this" but isn't using structures for multiple return values is better than using multiple out?



'Steve' on Mon, 13 Aug 2007 08:04:08 GMT, sez:

With the "this" keyword and some of the other changes (like the implicit typing) it seems that C# is moving slowly and crabwise from Java to JavaScript.



'Andrew Webb' on Mon, 13 Aug 2007 08:23:33 GMT, sez:

'ref' is essential. FxCop will complain if you have the effrontery to use 'out', and suggests that you find another way. 'this' is still in the future (for me at any rate since I usually avoid betas and CTPs).



'lb' on Mon, 13 Aug 2007 09:03:58 GMT, sez:

>FxCop will complain if you have the
>effrontery to use 'out', and suggests
>that you find another way

really? wow.

The danger there is that someone will simply change out to ref, thus forcing users to initialise the variable and pass in a reference when the param is always created within the given method.

I hate examples where the best way to avoid a warning is to write uglier code.

I think the use of 'out' as in TryParse is decent. But also -- i just googled the topic and wow some people don't like 'out' at all.


Here's a codeproject link

http://www.codeproject.com/useritems/alternative_2_out_keyword.asp



'Andrew Webb' on Mon, 13 Aug 2007 12:40:39 GMT, sez:

Leon,

Here's the Microsoft FxCop rule regarding out parameters:-

http://msdn2.microsoft.com/en-us/library/ms182131(VS.80).aspx

Note the Community Content at the bottom, which fits in with your comment on TryParse.




name


website (optional)


enter the word:
 

comment (HTML not allowed)


All viewpoints welcome. But the right to delete any post for any reason is reserved. Don't make me do it. Comments may be republished, emailed to your loved ones or printed and used as toilet paper. Who reads this legal bit anyhow?

TimeSnapper is a life analysis system that stores and plays-back your computer use. It makes timesheet recording a breeze, helps you recover lost work and shows you how to sharpen your act.

TimeSnapper won last year's Developer Competition at Larkware.com, and is used by over 10,000 people.

Articles

JSON Query Languages: 5 special purpose editors JSON Query Languages: 5 special purpose editors
What then, is b? What then, is b?
SQLike: A simple editor SQLike: A simple editor
Yet Another BizPlan Generator. Yet Another BizPlan Generator.
HOT GUIDS: A hot or not site for guids HOT GUIDS: A hot or not site for guids
How does life get better? One tiny hack at a time. How does life get better? One tiny hack at a time.
24 things to do, and 100 things *not* to do (yet) for building a MicroISV 24 things to do, and 100 things *not* to do (yet) for building a MicroISV
Venture capital won't kill Jeff Atwood, it will only make him Jeffer. Venture capital won't kill Jeff Atwood, it will only make him Jeffer.
A handy workflow image for newbie mercurial users A handy workflow image for newbie mercurial users
Fractal Feedback, a diversion into recreational programming Fractal Feedback, a diversion into recreational programming
Hump-Jumping: How the Education of Computer Science can be Saved, err, maybe. Hump-Jumping: How the Education of Computer Science can be Saved, err, maybe.
Suggested User Experience Improvements for DiffMerge Suggested User Experience Improvements for DiffMerge
SQL Style Extensions for C# SQL Style Extensions for C#
The Movie Hollywood (And My Wife) Doesn't Want You To See: Weekend at Jacko's The Movie Hollywood (And My Wife) Doesn't Want You To See: Weekend at Jacko's
Sysi: the ultimate administrators toolkit Sysi: the ultimate administrators toolkit

Archives .: secretGeek :: Complete Archives
TimeSnapper -- Automated Screenshot Journal TimeSnapper.com    
Version 3.3: true productivity boost

Next Action NextAction
Managing the top of your mind

World's Simplest Code Generator (html edition) World's Simplest Code Generator

25 steps for building a Micro-ISV 25 steps for building a Micro-ISV
3 minute guides -- babysteps in new technologies: powershell, JSON, watir, F# 3 Minute Guide Series
Universal Troubleshooting checklist Universal Troubleshooting Checklist
Top 10 SecretGeek articles Top 10 SecretGeek articles
ShinyPower (help with Powershell) ShinyPower
Now at CodePlex

Realtime CSS Editor, in a browser RealTime Online CSS Editor
Gradient Maker -- a tool for making background images that blend from one colour to another. Forget photoshop, this is the bomb. Gradient Maker


[powered by Google] 


How to be depressed How to be depressed
You are not inadequate.



Recommended Reading

The Best Software Writing I
The Business Of Software (Eric Sink)

Recommended blogs

Jeff Atwood
Joseph Cooney
Phil Haack
Scott Hanselman
Julia Lerman
Rhys Parry
Joel Pobar
OJ Reeves
Eric Sink

Aggregated Links

proggit
dzone
hacker news
dot net kicks

Human Link Machines

interesting finds
a continuous learner's weblog
arjan's world
weekly link post

LinkedIn profile
LogEnvy - event logs made sexy
ShuffleText - fuzzy search for .net
PC Smart Buys - Computer Hardware in Australia
 
home .: about .: sign up .: sitemap .: secretGeek RSS .: © Leon Bambrick 2006 .: privacy

home .: about .: sign up .: sitemap .: RSS .: © Leon Bambrick 2006 .: privacy