Re: Boxing and Unboxing ??
- From: "Jesse McGrew" <jmcgrew@xxxxxxxxx>
- Date: 16 Jan 2007 16:32:08 -0800
Bruce Wood wrote:
Jesse McGrew wrote:
Peter Olcott wrote:
"Jesse McGrew" <jmcgrew@xxxxxxxxx> wrote in message
news:1168918374.578116.244610@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Why should the programmer have to change his code just to enable an
optimization? Why not just say "your methods will run faster if you
don't modify large value-type parameters"?
This aspect is not a matter of optimization, it is a matter of preventing
programming errors. By using the [const] parameter qualifier it is impossible to
inadvertently change a function parameter that was not intended to be changed.
It's already impossible! Those errors don't exist today: if you pass a
value type into a method, any changes the method makes will only affect
the local copy inside that method; the original value in the caller
won't be changed.
The potential errors are purely a side effect of the *optimization*
you've been proposing, which is to pass "in" value types by reference
even though you don't expect to get a changed value back from the
method (which is the purpose of "ref" and "out" parameters). If you
weren't passing them by reference, there'd be no need for those
parameters to be read-only, because any changes would be local to the
method being called.
Jesse, I think that you misunderstand. C++ has a concept of "const" by
which you can pass a reference type, but can indicate that none of its
fields can be changed.
Maybe I am misunderstanding Peter's suggestion, then. I thought he was
talking about providing this for value types, such that 'void Foo(in
LargeStruct bar)' would pass bar by reference like the "ref" keyword,
but it would then be read-only from within Foo.
I can see the merits of applying a "const" attribute to reference types
- although read-only wrappers seem to be a decent solution too.
Jesse
.
- References:
- 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 ??
- From: Peter Olcott
- Re: Boxing and Unboxing ??
- From: Barry Kelly
- Re: Boxing and Unboxing ??
- From: Barry Kelly
- 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: 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: Jesse McGrew
- Re: Boxing and Unboxing ??
- From: Peter Olcott
- Re: Boxing and Unboxing ??
- From: Jesse McGrew
- Re: Boxing and Unboxing ??
- From: Bruce Wood
- Re: Boxing and Unboxing ??
- Prev by Date: Is there a way to call an API in unmanaged DLL from C# application in a way that the name of the API is dynamically generated
- 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
|