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

C#

C# Problem Overview


What is the [something] in

[something]
public class c1 {

}

called in C#? What does it do?

C# Solutions


Solution 1 - C#

That's an Attribute.

Solution 2 - C#

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

Solution 3 - C#

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#

C# Attributes. Please see this documentation.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBit HunterView Question on Stackoverflow
Solution 1 - C#Teoman SoygulView Answer on Stackoverflow
Solution 2 - C#JaredParView Answer on Stackoverflow
Solution 3 - C#Sam AxeView Answer on Stackoverflow
Solution 4 - C#Dmitry SavyView Answer on Stackoverflow