C# Refresh


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,

Create your playground on
This playground was created on, our hands-on, knowledge-sharing platform for developers.
Go to