[Tip: property]C#3.0 auto-implemented property

The pattern of properties encapsulating a single field is so common that C# 3.0 introduced auto-implemented properties. Here’s an example:

public int Rating { get; set; }

This Rating property encapsulates some value of type int. Because calling code doesn’t have access to the backing store anyway, knowing its name doesn’t matter. The C#
compiler will create an int field behind the scenes for us. 

The VS2008 Property Snippet
There’s another nice snippet in VS2008 that you can use for properties. Here’s how to
use it:
1. Click inside of the WebSite class. Create a class named WebSite if you haven’t
created it yet.
2. Type pro, press Tab, and you’ll see prop fill out. There are other property snippets,
but we only want to use prop right now.
3. Press Tab again to get the property snippet form. The highlight will be on the property
type, which defaults to int.
4. Type Web, and then press Tab. You’ll see WebSite fill out.
5. Press Tab and you’ll see control move to the next place in the form, which is the
name of the property.
6. Type BetaSite and then press Enter. You’ll see the cursor move to the end of the
snippet.
This created an automatic property. 

posted @ 2010-02-23 10:22  能巴  阅读(297)  评论(0编辑  收藏  举报