Information Technology Reference
In-Depth Information
get { return city; }
}
private readonly string city;
public string State
{
get { return state; }
}
private readonly string state;
public int ZipCode
{
get { return zip; }
}
private readonly int zip;
public Address3( string line1,
string line2,
string city,
string state,
int zipCode) :
this ()
{
this .line1 = line1;
this .line2 = line2;
this .city = city;
ValidateState(state);
this .state = state;
ValidateZip(zipCode);
this .zip = zipCode;
}
}
To c r e a t e a n i m m u t a b l e t y p e , y o u n e e d t o e n s u r e t h a t t h e r e a r e n o h o l e s
that would allow clients to change your internal state. Value types do not
support derived types, so you do not need to defend against derived types
modifying fields. But you do need to watch for any fields in an immutable
type that are mutable reference types. When you implement your con-
structors for these types, you need to make a defensive copy of that muta-
ble type. All these examples assume that Phone is an immutable value type
because we're concerned only with immutability in value types:
 
Search WWH ::




Custom Search