C# Attributes

Summary: in this tutorial, you’ll learn how to C# attributes to add metadata to your code and how to write a custom attribute.

Introduction to C# attributes

In C#, an attribute is a declarative tag that you can apply to classes, methods, properties, and other code elements.

An attribute provides additional information about the code element to which it is applied. For example, you can use an attribute to indicate how an object should be serialized.

All attributes class inherits from the System.Attribute class. Besides providing built-in attributes, you can create custom attribute classes that extend the System.Attribute class.

The following example demonstrates how to use a built-in Serializable attribute for the Person class. The Serializable attribute instructs .NET that the Person class can be serialized into a binary format:

[Serializable]
class Person
{
    public string? Name { get; set; }
    public sbyte? Age {  get; set; }
}Code language: C# (cs)

Why do you need C# attributes?

Attributes add metadata to your code so that .NET or other tools can use it at run time. For example, Visual Studio IDE uses attributes to provide IntelliSense and code completion suggestions. Also, the .NET runtime uses attributes to determine how to execute your code.

Attributes can also be useful for enforcing coding conventions. For example, you can use the Obsolete attribute to mark a method of a class obsolete. Then, Visual Studio can give a warning if you attempt to call the obsolete method.

Creating a custom attribute

The following program demonstrates how to create a custom attribute called Author:

[AttributeUsage(AttributeTargets.Class)]
class Author : Attribute
{
    public string Name
    {
        get; set;
    }

    public Author(string name)
    {
        Name = name;
    }
}

[Author("John Doe")]
class Person
{
}

class Program
{
    public static void Main(string[] args)
    {
        // get all custom attributes of the Person class
        var attributes = Attribute.GetCustomAttributes(typeof(Person));

        // Retrieve the author attribute
        var author = attributes.OfType<Author>().Single();
        Console.WriteLine(author.Name);
    }
}Code language: C# (cs)

Output:

John Doe

How it works.

First, define a custom attribute class named Author that inherits from the Attribute class:

[AttributeUsage(AttributeTargets.Class)]
class Author : Attribute
{
    public string Name
    {
        get; set;
    }

    public Author(string name)
    {
        Name = name;
    }
}Code language: C# (cs)

The Author class has a Name property and a constructor that takes a name parameter.

Also, we decorate the Author class with the [AttributeUsage(AttributeTargets.Class)] attribute, which specifies that the Author attribute can only be applied to classes.

Second, define a Person class and use the Author attribute to decorate it:

[Author("John Doe")]
class Person
{
}
Code language: C# (cs)

In the Author attribute, we set the Name property to "John Doe". Later, you can retrieve this information at runtime.

Third, show the Name property of the Author attribute applied to the Person class:

// Get all custom attributes of the Person class
var attributes = Attribute.GetCustomAttributes(typeof(Person));

// Retrieve the author attribute
var author = attributes.OfType<Author>().Single();
Console.WriteLine(author.Name);
Code language: JavaScript (javascript)

In the Main() method, we use the Attribute.GetCustomAttributes method to retrieve all custom attributes of the Person class.

Then, we use the OfType LINQ extension method to filter the list of attributes to get the one with the type Author.

Since the Person class has only one Author attribute, we use the Single method to retrieve it and display its Name property to the console.

Summary

  • Attributes are declarative tags that you can apply to classes, methods, and properties.
  • Attributes provide additional information to the code elements that it applies to.
  • Attributes use System.Attribute as the base class.
Was this tutorial helpful ?