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 class Verification and attribute Obsolete is applied to the method MyMethod.
Example
namespace House 
{
  [Test]  
  class Verification 
  {
    [Obsolete]
    public static void MyMethod()
    { 
    
    }
  }
}
Other C# job interview questions
C# for beginners
C# for beginners

Are you learning C#? Try our test we designed to help you progress faster.

Test yourself