Information Technology Reference
In-Depth Information
public struct
LogMessage2
{
private int
ErrLevel;
private string
msg;
public string
Message
{
get
{
return
(msg !=
null
) ?
msg :
string
.Empty;
}
set
{
msg =
value
;
}
}
}
Yo u s h o u l d u s e t h i s p r o p e r t y i n s i d e y o u r o w n t y p e . D o i n g s o l o c a l i z e s t h e
null
reference check to one location. The Message accessor is almost cer-
tainly inlined as well, when called from inside your assembly. You'll get
efficient code and minimize errors.
The system initializes all instances of value types to 0. There is no way to
prevent users from creating instances of value types that are all 0s. If pos-
sible, make the all 0 case the natural default. As a special case,
enums
used
as flags should ensure that 0 is the absence of all flags.
Immutable types are simple: After they are created, they are constant. If
you validate the parameters used to construct the object, you know that it
is in a valid state from that point forward. You cannot change the object's
internal state to make it invalid. You save yourself a lot of otherwise nec-
essary error checking by disallowing any state changes after an object has
been constructed. Immutable types are inherently thread safe: Multiple
readers can access the same contents. If the internal state cannot change,
there is no chance for different threads to see inconsistent views of the data.
Immutable types can be exported from your objects safely. The caller cannot