OdeToCode IC Logo

Automatic Properties

Wednesday, July 18, 2007

I wrote my first class with automatic properties in Orcas today...

[DataContract(Name="FoodFact")]
public class FoodFactMessage
{
    [
DataMember]
    
public int ID { get; set; }
    
    [
DataMember]
    
public string Name { get; set; }

    [
DataMember]
    
public int Calories { get; set; }
}

... then I found myself staring at the screen.

It's an interface!
No,it's a class!
Wait, it is a class!

I'd say the syntax is still growing on me.

I'm sure some people will say – why use properties at all? If you don't need special code in the get and set methods – why not just use a public field?

The quick answer is: reflection. There are many forms of magic that will only occur if you expose state using public properties.

Related Links