Steveluo’s Blog

Just another weblog

  • Categories

  • Archives

Back to Basics (2)

Posted by steveluo on March 8, 2009

see  MSDN’s Using Properties
“Properties have many uses: they can validate data before allowing a change; they can transparently expose data on a class where that data is actually retrieved from some other source, such as a database; they can take an action when data is changed, such as raising an event, or changing the value of other fields.”

a)  Auto-Implemented Properties (C#3.0 and up) :
      “the compiler creates a private, anonymous backing field can only be accessed through the property’s get and set accessors.
      The compiler creates backing fields for each auto-implemented property. The fields are not accessible directly from source” code.
// Auto-Impl Properties for trivial get and set
    public double TotalPurchases { get; set; }
    public string Name { get; set; }
    public int CustomerID { get; set; }

b)  Implement an Immutable Class That Has Auto-Implemented 
  “with auto-implemented properties, both a get and set accessor are required. You make the class immutable by declaring the set accessors as private. However, when you declare a private set accessor, you cannot use an object initializer to initialize the property. You must use a constructor or a factory method.”


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: