Effective C # Principle 7: Select constant atomic value type data

Source: Internet
Author: User
Tags constant

The constant type (immutable types) is actually very simple, but they are created and they are fixed. If you verify some of the parameters that are ready to be used to create an object, you know it is in the validation state from the previous point of view. You cannot modify an object's internal state to make it invalid. After an object is created, you must carefully protect the object, or you will have to do error validation to prevent any state from changing. A constant type is inherently thread-complete: Multiple visitors can access the same content at the same time. If the internal state cannot be modified, it is not possible for different threads to provide an opportunity to view inconsistent views of the data. The constant type can be safely exposed from your class. The caller cannot modify the internal state of the object. A constant type can work well on a collection based on hash codes. The value returned in the Object.GetHashCode () method must be the same for the same instance (see Principle 10), which is where the constant type always succeeds.

Not all types can be of constant type. If it can, you need to clone an object to modify the state of any program. This is why it is recommended to use constant type and atomic type data at the same time. Decompose your object into a single physical structure of nature. An address type is a simple thing that is made up of a number of related fields. Changing one of these fields is likely to mean modifying the other fields. A customer type is not an atomic type, and a customer type may contain a lot of small chunks of information: address, first name, one or more phone numbers. Any information block that is not associated with each other can be changed. A customer may change the phone number without moving. Another client may keep the original phone number if he has moved home. It is also possible that a client changed his or her name without moving or changing the phone number. A customer type is not an atomic type; it is composed of several different constant components: address, name, and a collection of phone numbers that appear in pairs. The atomic type is a single entity: You naturally replace the entity content with atomic types. This exception will change one of its constituent fields.

The following is a typical implementation of a variable address class:

//mutable address structure.
Public struct address
{
private string _line1;
private string _line2;
private String _city;
private string _state;
Private int _zipcode;  
//Rely on the default system-generated
//constructor.
public string Line1 The
{
get {_line1;}
Set {_line1 = value;}
}
public string Line2
{
get {return _line2;}
Set {_line2 = value;}
}
public string city
{
get {_city;}
Set {_city= value;}
}
public string state
{
get {_state;}    
Set
{
Validatestate (value);
_state = value;
}
}
public int ZipCode
{
get {_zipcode;}    
Set
{
Validatezip (value);
_zipcode = value;
}

//other details omitted.
}
//Example Usage:
Address a1 = new address ();
A1. Line1 = "S. Main";
A1. City = "Anytown";
A1. State = "IL";
A1. ZipCode = 61111;
//Modify:
A1. City = "Ann Arbor"; ZIP, state invalid now.
A1. ZipCode = 48103; State still invalid now.
A1. State = "MI"; Now fine.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.