Always will be ready notify the world about expectations as easy as possible: job change page
Jun 29, 2023

Attributes and Decorators in C#

Megha Prasad

Attributes and Decorators in C#

Attributes and decorators, which allow you to attach metadata to classes, properties, and methods.


Attributes in C# allow you to attach metadata to your classes, methods, and other code elements. This metadata can then be used at runtime to modify the behavior of your code. Attributes are applied to code elements using square brackets, and they can contain additional information such as parameters.

For example, you can use the [Serializable] attribute to indicate that an object can be serialized and saved to disk. The [Obsolete] attribute can be used to indicate that a method or class should not be used anymore. You can also create your own custom attributes to add information to your code.

To create a custom attribute in C#, you need to create a class that derives from the System.Attribute class. This class can contain properties and constructor parameters that allow you to pass additional information to the attribute.

An attribute is a class that derives from the System.Attribute base class and is used to store metadata about a class, property, or method. Attributes are typically used to provide information about the target, such as its intended use, permissions required, or other relevant details. For example, the Obsolete attribute is used to mark a class, property, or method as deprecated and inform the compiler that it should generate a warning if the deprecated item is used.

Here’s an example of a custom attribute:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
public class MyAttribute : Attribute
    public string Message { get; set; }
    public MyAttribute(string message)
        Message = message;

In this example, the MyAttribute class contains a property Message and a constructor that takes a string argument. The AttributeUsage attribute specifies the targets that the attribute can be applied to, in this case, classes and methods. The AllowMultiple property allows the attribute to be applied multiple times to the same code element.

To use the attribute, simply apply it to a class or method using square brackets:

[MyAttribute("Hello World")]
public class MyClass

You can also retrieve the values of attributes at runtime using reflection. Reflection is a feature in C# that allows you to inspect and manipulate the metadata of types at runtime. With reflection, you can inspect the properties, methods, and events of a type, and even create and execute code dynamically. To retrieve the values of attributes, you can use the GetCustomAttributes method of the Type class.


Decorators are a feature in C# that provide a way to add additional behavior to existing classes and methods. Unlike attributes, which are applied using square brackets, decorators are applied using the decorator keyword. Decorators allow you to wrap existing code elements with additional functionality, without modifying the original code.

The decorator class must have a reference to the code element it decorates and can modify its behavior in various ways, such as adding pre- and post-processing logic, logging, caching, and more.

For example, you can create a decorator to log the execution time of a method. Here’s an example of a decorator in C#:

public class Logger
    public void LogBefore(string message)
        Console.WriteLine($"Logging before execution: {message}");

    public void LogAfter(string message)
        Console.WriteLine($"Logging after execution: {message}");

public class LoggingDecorator
    private Logger _logger;

    public LoggingDecorator(Logger logger)
        _logger = logger;

    public void Decorate(Action action)
        _logger.LogBefore("Starting method execution");
        _logger.LogAfter("Finished method execution");

In this example, the Logger class contains two methods for logging messages. The LoggingDecorator class takes a reference to a Logger object in its constructor and implements the Decorate method, which takes an Action as an argument. The Decorate method logs a message before and after the execution of the action.

To use the decorator, you can pass a method to the Decorate method, like this:

var logger = new Logger();
var decorator = new LoggingDecorator(logger);

decorator.Decorate(() => Console.WriteLine("Hello World"));

This will log a message before and after the execution of the method, like this:

Logging before execution: Starting method execution
Hello World
Logging after execution: Finished method execution

Decorators can be very powerful, especially when used in combination with other language features such as generics and delegates. They allow you to add behavior to your code in a modular and composable way, making it easier to maintain and extend over time.

Oct 21, 2023
Background Functional Decomposition is good in a procedural programming environment. It's even useful for understanding the modular nature of a larger-scale application. Unfortunately, it doesn't translate directly into a class hierarchy, and this is where the problem begins. In defining...
Jan 28, 2023
Author: Rokas
Prerequisites: VSCode and .Net 7 installed. Don't need to copy the code, GitHub link provided in the end. Open up a terminal in VSCode or any IDE of your choice, run: dotnet new console 2 files will appear, default Program.cs...
Apr 24, 2022
Author: HungryWolf
What is MediatR? Why do we need it? And How to use it? Mediator Pattern - The mediator pattern ensures that objects do not interact directly instead of through a mediator. It reduces coupling between objects which makes it easy...
Oct 20, 2022
Author: Ricardo Griffith
Schedule and run background jobs using this powerful framework Disclaimer: the image mentioned in this article is currently marked as preview. Opinions expressed therein are solely my own and do not express the views or opinions of my employer. Hangfire...
Send message
Your name

© 1999–2024 WebDynamics
1980–... Sergey Drozdov
Area of interests: .NET Framework | .NET Core | C# | ASP.NET | Windows Forms | WPF | HTML5 | CSS3 | jQuery | AJAX | Angular | React | MS SQL Server | Transact-SQL | ADO.NET | Entity Framework | IIS | OOP | OOA | OOD | WCF | WPF | MSMQ | MVC | MVP | MVVM | Design Patterns | Enterprise Architecture | Scrum | Kanban