Information Technology Reference
In-Depth Information
private string
name;
private decimal
revenue;
public
Customer(
string
name)
{
this
.name = name;
}
public string
Name
{
get
{
return
name; }
// Name is readonly
}
public decimal
Revenue
{
get
{
return
revenue; }
set
{ revenue =
value
; }
}
public override int
GetHashCode()
{
return
name.GetHashCode();
}
public
Customer
ChangeName(
string
newName)
{
return new
Customer
(newName) { Revenue = revenue };
}
}
ChangeName() creates a new Customer object, using the constructor and
object initialize syntax to set the current revenue. Making the name
immutable changes how you must work with customer objects to modify
the name:
Customer
c1 =
new
Customer
(
"Acme Products"
);
myDictionary.Add(c1, orders);
// Oops, the name is wrong:
Customer
c2 = c1.ChangeName(
"Acme Software"
);
Order
o = myDictionary[c1];