That only applies if null is not a valid state for the reference type
fields that he has. If you can explicitly set the value to null, then you
still need the flag.
On the flip side, as of .NET 2.0, you could use a nullable type for the
backing field (assuming null is not allowed as a value in the range of
values) for value types, and then check for null to see if the value has
been set (while the property exposed is not nullable).
You could get away with this by using a context bound object, and
intercepting the calls, but that is a lot of work, and there is going to be
a bit of overhead to intercept the calls. However, it would allow for a
more concise implementation (the interception layer for the context bound
object would handle whether or not a property was explicitly set).
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.comwrote in
message news:%2***************@TK2MSFTNGP04.phx.gbl...
Hi,
<dv*****@gmail.comwrote in message
news:11**********************@d55g2000hsg.googlegr oups.com...
>...but for my purposes, that's way too verbose. I need a way to
achieve that functionality, but write the class much more concisely.
Does anybody know a way to do this while allowing a shorter class
definition, such as the one below?
class C
{
// Is there any way to know when
// MyObj is set?
public object MyObj;
}
In general there is no way of doing it, if you only have references types
you could check if they are not null. This fails for valued types, as they
always have a value. Additionally the method will also fails with
references types that the class initialize in the constructor.
You will have to use the verbose method I think