Home
C#
Attribute Examples
Updated Aug 7, 2024
Dot Net Perls
Attribute. The C# language can be extended. Attributes extend classes and types. This C# feature allows you to attach declarative information to any type.
class
Attributes are accessed at compile-time or runtime through the metadata. In our C# programs we then can handle types based on their attributes.
Conditional
enum Flags
First example. This program provides an example of the Obsolete attribute. The Obsolete attribute is a way to declare that a method is deprecated and should be avoided.
Obsolete
Tip The actual type referenced is "ObsoleteAttribute," but you can omit the word Attribute.
Tip 2 The attribute modifies the compiler's view of the Program.Text method. It doesn't affect runtime.
using System;

class Program
{
    static void Main()
    {
        // Warning: 'Program.Test()' is obsolete
        Test();
    }

    [Obsolete]
    static void Test()
    {
    }
}
'Program.Test()' is obsolete
Syntax. An attribute is a class that is derived from the Attribute class through inheritance. Often, we put an attribute on the class itself.
Note This provides a way to specify what kind of types the attribute applies to, among other options.
Note 2 We specify to use the attribute with square brackets and the name of the attribute, omitting the word "Attribute" on the end.
Detail This is an attribute class that can only be attached to class types—not fields, methods, or properties.
using System;

/// <summary>
/// An attribute that can only be attached to classes.
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class PerlsAttribute : Attribute
{
}

/// <summary>
/// Use short syntax to reference attribute.
/// </summary>
[Perls]
class Example1
{
}

/// <summary>
/// Use long syntax to reference attribute.
/// </summary>
[PerlsAttribute]
class Example2
{
}

class Program
{
    static void Main()
    {
        // For compilation.
    }
}
Members. This example includes a string member field, a property accessor to that field, and a constructor. With this version of PerlsAttribute, we must use a string parameter.
using System;

/// <summary>
/// Attribute.
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class PerlsAttribute : Attribute
{
    /// <summary>
    /// Stores string field.
    /// </summary>
    string _id;

    /// <summary>
    /// Attribute constructor.
    /// </summary>
    public PerlsAttribute(string id)
    {
        this._id = id;
    }

    /// <summary>
    /// Get Id.
    /// </summary>
    public string Id
    {
        get { return this._id; }
    }
}

/// <summary>
/// Apply attribute.
/// </summary>
[Perls("Dot")]
class Example1
{
}

class Program
{
    static void Main()
    {
        // For compilation.
    }
}
Positional, named. The C# specification recommends that named parameters be used. They are not as likely to be invalidated when the attribute declaration changes.
Detail With positional parameters, you rely on the position of the parameters in the constructor.
Constructor
However With named parameters, you simply rely on having specified the correct name.
using System;

/// <summary>
/// Attribute.
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class PerlsAttribute : Attribute
{
    /// <summary>
    /// String field.
    /// </summary>
    string _id;

    /// <summary>
    /// Attribute constructor.
    /// </summary>
    public PerlsAttribute()
    {
    }

    /// <summary>
    /// Get and set.
    /// </summary>
    public string Id
    {
        get { return this._id; }
        set { this._id = value; }
    }
}

/// <summary>
/// Set property in the attribute.
/// </summary>
[Perls(Id = "Sam")]
class Example1
{
}

class Program
{
    static void Main()
    {
        // For compilation.
    }
}
AllowMultiple. By default an attribute can only be specified a single time on a member or type declaration. To bypass this restriction, use AllowMultiple.
Summary. Custom attributes are of fairly limited use. But they provide many options for extensibility. With them we attach declarative information to members and types.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Aug 7, 2024 (simplify).
Home
Changes
© 2007-2025 Sam Allen