Saturday, October 13, 2012

C# Attributes

C# Attributes [attribute] - Java referes to this as Accessors @accessor

Attributes are used to define additional functionality to methods ,classes properties, etc. Here is the full list that you can assign attributes to.
You can do a variety of things using attributes (custom or already made) such as validate fields, change the return value, add metadata, etc.

Some examples of using attributes are when Linq to SQL assigns classes to a Table or assignes properties to table columns.
[Table(Name="Orders")]
class Order
[Column(Name="OrderDate", CanBeNull=true)]
 public DateTime? OrderDate { get; set; }


Basic information on creating custom attributes can be read here.
A great example of how you can validate fields using attributes is here.
A great example of reading the assigned attributes values can be read here or here.