What is an attribute in C#?
Experience Level: Junior
Tags: C#
Answer
- In C#, an attribute is like a tag using which you can add additional information to a class or method.
- The attribute is applied to a class or method by writing the attribute name into the square brackets before the class or method definition.
- In the example below you can see how the attribute
Test
is applied to the classVerification
and attributeObsolete
is applied to the methodMyMethod
.
namespace House
{
[Test]
class Verification
{
[Obsolete]
public static void MyMethod()
{
}
}
}