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

What To (Really) Do If You Find Out Your Parents Are Using Vista (redux) What To (Really) Do If You Find Out Your Parents Are Using Vista (redux)
What To Do If You Find Out Your Parents Are Using Vista What To Do If You Find Out Your Parents Are Using Vista
Sample Code From Text-Adventure Game Platforms Sample Code From Text-Adventure Game Platforms
TimeSnapper 3.0 -- an interactive, bubbling cauldron of possibilities TimeSnapper 3.0 -- an interactive, bubbling cauldron of possibilities
The laptop compubody sock The laptop compubody sock
Everything that's bad for you is suddenly good for you! Everything that's bad for you is suddenly good for you!
Everything I know about Code Reviews I learnt from Star Wars (and JCooney) Everything I know about Code Reviews I learnt from Star Wars (and JCooney)
Syntax highlighting of strings Syntax highlighting of strings
Google AppEngine: evil virus or viral evil? Google AppEngine: evil virus or viral evil?
Workflow software: I'm calling the bluff. Workflow software: I'm calling the bluff.

Archives .: secretGeek :: Complete Archives :.
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
Top 10 SecretGeek articles Top 10 SecretGeek articles

Downloads

TimeSnapper -- Automated Screenshot Journal TimeSnapper.com    
Version 2.5: with password protection

ShinyPower (help with Powershell) ShinyPower
Now at CodePlex

Next Action NextAction
Managing the top of your mind



[powered by Google] 

Thai Erawan, Brisbane Restaurant, delicious thai food in paddington Thai Erawan, Brisbane Restaurant
World's Simplest Code Generator (html edition) World's Simplest Code Generator
Gradient Maker -- a tool for making background images that blend from one colour to another. Forget photoshop, this is the bomb. Gradient Maker
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
Reginald Braithwaite
Joseph Cooney
Phil Haack
Scott Hanselman
Julia Lerman
Joel Pobar
Eric Sink
Joel Spolsky
Des Traynor

Aggregated Links

programming.reddit.com
dzone
dot net kicks

Human Link Machines

interesting finds
a continuous learner's weblog
arjan's world
n links today
new and notable
morning coffee
learning .net
weekly link post
(my del.icio.us account)

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

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