What is the thing in square brackets that comes before a C# class declaration called? [closed]

Solution 1:

That's an Attribute.

Solution 2:

This is known as an attribute application / usage. It associates an instance of a given Attribute with a type. These are user definitable items. For example

[AttributeUsage(AttributeTargets.All)]
public class ExampleAttribute : System.Attribute {
  public ExampleAttribute() { }
}

This is an attribute which can be applied on ever place an attribute is legal

// Assembly level
[assembly: Example]

// Class
[Example]
public class C1 {
  // Field
  [Example]
  public int m_field;

  // Method
  [Example]
  public void Test() { }
}

More locations are possible but hopefully this gets the general idea across. You may also want to check out this tutorial

  • http://msdn.microsoft.com/en-us/library/aa288454(v=vs.71).aspx

Solution 3:

Its called an Attribute. A class that ends in "Attribute", and inherits from Attribute:

public class SomethingAttribute : Attribute {

}

If you are creating one, be sure to look up the AttributeUsageAttribute class.

Solution 4:

C# Attributes. Please see this documentation.