C# Refresh

Nonsultant
53.4K views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Attributes in C#

Attributes gives us the ability to associating metadata, or declarative information, with code (assemblies, types, methods, properties etc.). This is in C# done using [* and *], if you would like to add an attribute to a property:

[Description("This is a description of my property")]
public string MyProperty { get; set;}

Using reflection is it possible to retrieve the attributes. We are especially going to use this when working with Object-relational mapping (ORM) later in the course.

Example of reading an attribute using reflection:

.NET comes with a range of build in attributes, among these are:

Custom attributes

It's possible to define your own attributes. This done by creating a class which inherits from Attribute,

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content