|
-
October 16th, 2014, 10:22 AM
#3
Re: Implicit Conversion Operators and Value Types
well i liked the article
in general the whole point of the article could have been sumed up to
rule of thumb
don't use implicit operators on references
use explicit operators or conversion methods on references
he makes some good points to back it up as well
That said, to what value would this evaluate?
object.ReferenceEquals(wrapper, earlGray);
though that said your average programmer probably is not using that
then again a new programmer might be using it correctly.
either way we shouldn't miss his point's or dismiss it.
which although he maybe didn't state so well
implicit conversion from one reference to another can be dangerous down the road
to try explain it simply from another angle ill try to state his case as
what we expect vs what we could end up with
ok so lets say we have two types A and B
they could be value or ref....
(we should expect a slightly different behavior from each)
A a;
in the case of a value you could also imagine this a= or a.feild = something;
in the case of a reference you might imagine this a = new A(something);
then we have another reference or value B
B b;
so whats the differences, right off the bat
what should we should expect if the above is reference type or value type.
below with a reference type both point to the same memory location
though there are two separate references they point to the same location
b = a;
below with a value type both do not point to the same location
there are two copy's at two different locations containing the same value
b = a;
so his first point is
behaviorally if you change the implicit operators for reference types
or cause them to act as though it is a value behaviorally
by using implicit operators
then, with a reference type, the behavior expected of a = b doesn't work the same
ergo
pointing to the same memory location like it/you would expect it should
instead
you get, two refs, that point to two different locations, with the same values
someone might come along to use it like every other reference they are used to
but now it works like a value
they dont' know why... they get bugs... they cant figure it out... = arggahhh
soooo he's saying.... that's bad... and ya in general he's right it is.
his second point is
if we did have to use a class...
then to convert to say a value or another reference type
we should have a method that makes it explicitly obvious
so then the way to do that is to not actually use the implicit operator 
n instead make a method that makes it clear
or use a (explicit operator) ? to cast i dunno
point is make it so at least its somewhat clear what is being done
Last edited by willmotil; October 16th, 2014 at 11:20 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|