Re: Boxing and Unboxing ??
- From: "Peter Olcott" <NoSpam@xxxxxxxxxxxxx>
- Date: Sat, 13 Jan 2007 21:55:25 -0600
"Barry Kelly" <barry.j.kelly@xxxxxxxxx> wrote in message
news:bvriq294cgvtb0ugqu410ptj5mopov8k1q@xxxxxxxxxx
Peter Olcott wrote:
You can, but in a strictly downwards (call stack) fashion, via the 'ref'So I can call one member function from another member function of a different
modifier on arguments. You can't safely store the address.
class and pass the address of the a struct to the second member function so
that
the second member function can directly update the contents of the struct,
without any boxing and unboxing overhead?
Yes.
If the answer is yes, then what is the
syntax for doing this?
void Foo(ref MyStruct value) { } // declaration
// ...
MyStruct myStructValue;
// ...
Foo(ref myStructValue); // usage
There is also an 'out', which is similar but (a) argument need not be
definitely assigned when passed in (but it will be definitely assigned
after the call) and (b) it is treated as unassigned in the body of the
method taking the parameter, and will be so treated until it's assigned
(and must be assigned before the method returns).
But be sure to measure that:
1) MyStruct being a struct (value type) is the right thing to do.
Typically, if sizeof(MyStruct) is greater than (say) 16 bytes, it's
looking like it might be too big. Of course, there are exceptions to
this, like in all performance work. Measure, etc.
2) The savings by passing by-ref outweigh the fact that it's a mutable
reference. In other words, beware that there's no const by-ref
mechanism.
There is no inherent reason why this could not be added to the language as a
compile time feature later on. It might be simpler to stick with the established
convention and simply make an [in] equivalent of an [out] parameter, instead of
using the somewhat less obvious [const]. There would be no reason to distinguish
between [in] by reference and [in] by value, they could all be passed by
reference, or anything larger than [int] could always be passed by reference.
It is good to know that aggregate data can be passed by reference without the
boxing and unboxing overhead, if need be.
-- Barry
--
http://barrkel.blogspot.com/
.
- Follow-Ups:
- Re: Boxing and Unboxing ??
- From: Jon Skeet [C# MVP]
- Re: Boxing and Unboxing ??
- References:
- Re: Boxing and Unboxing ??
- From: Jesse McGrew
- Re: Boxing and Unboxing ??
- From: Peter Olcott
- Re: Boxing and Unboxing ??
- From: Jesse McGrew
- Re: Boxing and Unboxing ??
- From: Peter Olcott
- Re: Boxing and Unboxing ??
- From: Barry Kelly
- Re: Boxing and Unboxing ??
- From: Peter Olcott
- Re: Boxing and Unboxing ??
- From: Bruce Wood
- Re: Boxing and Unboxing ??
- From: Peter Olcott
- Re: Boxing and Unboxing ??
- From: Barry Kelly
- Re: Boxing and Unboxing ??
- From: Peter Olcott
- Re: Boxing and Unboxing ??
- From: Barry Kelly
- Re: Boxing and Unboxing ??
- Prev by Date: Re: Is it possible to hide methods to certain classes?
- Next by Date: Re: Boxing and Unboxing ??
- Previous by thread: Re: Boxing and Unboxing ??
- Next by thread: Re: Boxing and Unboxing ??
- Index(es):
Relevant Pages
|