Tuesday 28 June 2011

C# Property vs Attribute

Ever wondered what these two terms really mean?

Well, its a potentially lengthy discussion...but after a quick research, the general synopsis can be summed up with a couple of sentences:

1. Properties relate to the "Has a..." relationship and Attributes relate to the "Is a..." relationship
eg A Car "Has a" set of Doors whereas a Car "Is..." Drivable

2. In OOP we are modelling 'things' and we have 'mechanisms' to model them with. In this context Properties are used to help 'model the things' and attributes are used to 'describe the mechanisms'
eg A Car has a property Color and the class used to model the car has an attribute 'Serializable'

[Serializable]
public class Car{
    ...
    public string Color{ ... }
}

If it's all still confusing for you see this great post by Eric on 'Properties vs Attributes'

No comments:

Post a Comment