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. Aim for constructiveness. Comments may be republished, emailed to your loved ones or printed and used as toilet paper. Also, I get particularly nasty on comment spam. It's not worth even trying to post comment spam here -- your html is escaped, and your links are given a rel='nofollow'. By attempting to post a comment, you understand that if the comment is considered spam, at my absolute discretion, your IP address may be used as the target of a prolonged distributed denial of service attack. Your electricity might suddenly stop working. Your car tyres will go mysteriously flat. You will suffer permanent hairloss. Your dreams will be filled with terrifying monsters. And in any case I reserve the right to record and publish your IP address.

 

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.

 

NimbleText - FREE text manipulation and data extraction

NimbleText is a Powerful FREE Tool

Use it for:

  • extracting data from text
  • manipulating text
  • generating code

It makes you look awesome. Use it right now! Go on! Hurry! Don't walk, run!

 

Articles

Just Wally Just Wally
The Correct Order for a First Time Viewing of The Lord Of The Rings The Correct Order for a First Time Viewing of The Lord Of The Rings
A new era for Android. A new era for Android.
Mind-boggling Demo of New Gaming Genre, aka Folder-Based Hangman, aka Fun with Recursion Mind-boggling Demo of New Gaming Genre, aka Folder-Based Hangman, aka Fun with Recursion
Got CSV in your javascript? Use agnes. Got CSV in your javascript? Use agnes.

Archives Complete secretGeek Archives

TimeSnapper -- Automated Screenshot Journal TimeSnapper: automatic screenshot journal
NimbleText -- World's Simplest Code Generator NimbleText: Code Generator, Text Manipulator, Data Extractor

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 little schemer


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
Thomas White
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
Computer, Unlocked. A rapid computer customization resource
PhysioTec, Brisbane Specialist Physiotherapy & Pilates
 
home .: about .: sign up .: sitemap .: secretGeek RSS .: © Leon Bambrick 2006 .: privacy

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