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.
|