Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

C# automatic properties

C# 3.0 makes it very convenient to create properties that doesn't carry any functionalities besides returning value of attribute and assigning value to it. This new feature is called automatic properties.

Suppose we have a class with big load of properties that just return attribute values and assign given values to them. Something like you can see in following code.


public class Product
{
    private string description;
    private Int32 size;
    private decimal price;

    public string Description
    {
        get { return description; }
        set { description = value; }
    }

    public Int32 Size
    {
        get { return size; }
        set { size = value; }
    }

    public decimal Price
    {
        get { return price; }
        set { price = value; }
    }

    ...
}

All three properties given here are following the same pattern: get returns value of attribute, set assigns value to attribute. There is no more functionality in the bodies of these properties. This pattern gave an idea to language authors. Why not to use some shorter syntax for this kind of properties and let compiler to do all the dirty work? So, here is what we can use in C# 3.0.


public class Product
{
    public string Description { get; set; }
    public Int32 Size { get; set; }
    public decimal Price { get; set; }
    ...
}

Automatic properties are compiled as properties, not as public attributes of class. This way we are able to add functionality to properties later and we doesn't break connections between class and the other classes.


kick it on DotNetKicks.com pimp it Progg it Shout it

Comments

Hecgo.com » Propiedades automáticas en C# 3.0 said:

Pingback from  Hecgo.com  » Propiedades automáticas en C# 3.0

# November 15, 2007 10:51 PM

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# July 12, 2009 2:11 PM

DotNetBurner - C# said:

DotNetBurner - burning hot .net content

# July 12, 2009 2:14 PM

PimpThisBlog.com said:

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

# July 12, 2009 2:15 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# July 12, 2009 2:17 PM

progg.ru said:

Thank you for submitting this cool story - Trackback from progg.ru

# July 12, 2009 2:18 PM

Weekly Link Post 102 « Rhonda Tipton’s WebLog said:

Pingback from  Weekly Link Post 102 « Rhonda Tipton’s WebLog

# July 19, 2009 8:51 PM

George said:

Why not just make the data member public with a capitalized name (ala property syntax)? Then if you wanted to add behavior, you can add formal property syntax and change the data member to be private with a lower case name. Same diff: you don't break clients that use property syntax.

# August 14, 2009 5:14 PM

PhillDoc said:

Cool article as for me. I'd like to read more concerning that topic.

# October 22, 2009 1:25 AM

Gunnar Peipman's ASP.NET blog said:

Next version of .Net Framework brings some new features also to VB.NET. One of those new features is

# November 1, 2009 7:08 AM

Community Blogs said:

Next version of .Net Framework brings some new features also to VB.NET. One of those new features is

# November 1, 2009 7:15 AM