开发者

Automatic Properties (LINQ)

Would someone be able to explain or provide a link to a page that describes what Automatic Properties are (in relat开发者_JAVA技巧ion to LINQ) in lamens terms please


Automatic properties - better call them "auto-implemented properties", are a new syntax sugar added in latest C# versions as some comment pointed out.

It consist in a property that declare its accessors without body and C# compiler creates the corresponding private fields for you:

public string Name
{
    get;
    set;
}

Note that this isn't an abstract member, becase it'd be marked with the appropiate attribute "abstract"!

Additionally to that, these accessors, as non-auto-implemented ones, can have visibility attributes: private, internal, public (default behavior):

public string Name
{
     private get;
     internal set;
}


Here is an automatic property in C# 3.0:

public class Person 
{    
   public string FirstName  { get; set; }
   public string LastName  { get; set; }
}  

compared to a non automatic property:

public class Person 
{    
   string _FirstName;
   string _LastName;

   public string FirstName 
   {
        get { return _FirstName; }
        set { _FirstName = value; }
   }

   public string LastName 
   {
        get { return _LastName; }
        set { _LastName = value; }
   }
} 

Here's the post by Dan Wahlin on automatic properties, from where I got the above code.


Refer to the following:

Auto-Implemented Properties

Using Automatic Properties in LINQ

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜