Re: Boxing and Unboxing ??

Tech-Archive recommends: Fix windows errors by optimizing your registry




"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'
modifier on arguments. You can't safely store the address.

So I can call one member function from another member function of a different
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/


.



Relevant Pages

  • Re: Boxing and Unboxing ??
    ... So I can call one member function from another member function of a different ... the second member function can directly update the contents of the struct, ... void Foo(ref MyStruct value) // declaration ... The savings by passing by-ref outweigh the fact that it's a mutable ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Class/struct and Marshal.SizeOf
    ... > See my notes inline: ... A struct or a class that holds an array of x elements of a reference ... public struct MyStruct ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Threads on class,
    ... > member function as the thread procedure and thus you don't need the ... DWORD ThreadId; ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: Threads on class,
    ... > member function as the thread procedure and thus you don't need the ... DWORD ThreadId; ...
    (microsoft.public.vc.language)
  • Re: gcnew does not generate an object instance?
    ... If you define a reference type variable without a hat, ... trying to invoke members on it will cause an exception. ... If I declare p_usbapi as an object without the hat, see CODE#1, ... // some time later a member function is called: ...
    (microsoft.public.dotnet.languages.vc)