tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

Attribute on attribute itself

10 Feb 2022 1 mins C#

Today I learned that you could use attribute, in C#, on itself. 🤯 Not sure why would I do that, but it’s cool.

This simple C# code…

public class MyAttribute : Attribute
{ }

…compiles into this IL just fine.

.class public auto ansi beforefieldinit MyAttribute
    extends [System.Private.CoreLib]System.Attribute
    .custom instance void MyAttribute::.ctor() = (
        01 00 00 00
    // Methods
    .method public hidebysig specialname rtspecialname 
        instance void .ctor () cil managed 
        // Method begins at RVA 0x2050
        // Code size 7 (0x7)
        .maxstack 8

        IL_0000: ldarg.0
        IL_0001: call instance void [System.Private.CoreLib]System.Attribute::.ctor()
        IL_0006: ret
    } // end of method MyAttribute::.ctor

} // end of class MyAttribute

I, obviously, never needed that before. And I don’t know what I would use it realistically for. But I’ll put this into “cool facts for beer talk” drawer.

Profile Picture Jiří Činčura is .NET, C# and Firebird expert. He's focused on data and business layers, language constructs, parallelism, databases and performance. He's Microsoft Most Valuable Professional and frequent speaker. You can read his articles, guides and tips and tricks at