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

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
Movie: Priest Academy Movie: Priest Academy
Inspirational Rat Story Inspirational Rat Story
A face-melting DSL that allows programming ON the iPhone (and iPad) A face-melting DSL that allows programming ON the iPhone (and iPad)
The secretGeek Disaster Recovery plan The secretGeek Disaster Recovery plan
Save KNVTn! Before it's too late Save KNVTn! Before it's too late
The Ultimate Agent of WERF Destruction The Ultimate Agent of WERF Destruction
The new prisoner's dilemma The new prisoner's dilemma
Original Premise for a road movie Original Premise for a road movie
What's a better game than Devshop? What's a better game than Devshop?
DevShop: The Cool Game that Makes Development Look Fun DevShop: The Cool Game that Makes Development Look Fun
Should be purple Should be purple
Kitchen Agile Kitchen Agile
Perhaps Perhaps "Go" is the new Visual Basic
zen-coding: turn those CSS selectors upside down zen-coding: turn those CSS selectors upside down
Debugging: It's all about finding Albuquerque. Debugging: It's all about finding Albuquerque.
The Real-Time online JQuery Editor The Real-Time online JQuery Editor
HTML5, a 3 minute guide HTML5, a 3 minute guide
Developer Codpieces Developer Codpieces
Agile for one: The Personal Story 'Wall' In Action Agile for one: The Personal Story 'Wall' In Action
Never work with thick people. Never work with thick people.
Cosmo: project status panel Cosmo: project status panel
Windows Search in Japan Windows Search in Japan
Project Management Zen Project Management Zen
Continuous Integration, Plugins and Going Too Far Continuous Integration, Plugins and Going Too Far
The Rules of Stand Up The Rules of Stand Up
Sydney International Airport: Stupid, Criminal, or Criminally Stupid? Sydney International Airport: Stupid, Criminal, or Criminally Stupid?
God No! ...The ReBuilder God No! ...The ReBuilder
Matt, The Office Mortar Matt, The Office Mortar
'Outlook style' rules for Subversion 'Outlook style' rules for Subversion
Really deep linking: Url + regex Really deep linking: Url + regex
hExcel -- A Hexagonal Spreadsheet hExcel -- A Hexagonal Spreadsheet
Is the remote control a thing of the past? Is the remote control a thing of the past?
The Utterly Thorough Guide To Awesome Application Compatibility on Windows 7. The Utterly Thorough Guide To Awesome Application Compatibility on Windows 7.
Astounding Hyperlinked Noticeboard Astounding Hyperlinked Noticeboard
Three Questions About Each Bug You Find Three Questions About Each Bug You Find
Recursing over the Pareto Principle... Recursing over the Pareto Principle...
Sometimes, The Better You Program, The Worse You Communicate. Sometimes, The Better You Program, The Worse You Communicate.

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
Reginald Braithwaite
Joseph Cooney
Phil Haack
Scott Hanselman
Julia Lerman
Rhys Parry
Joel Pobar
OJ Reeves
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