An attribute is a declarative tag that is used to convey information to runtime about the behaviors of various elements like classes, methods, structures, enumerators, assemblies etc. in your program.
To set an attribute −
[attribute(positional_parameters, name_parameter = value, ...)] Element
Here, the name of the attribute and values come inside [ ] positional parameters allow you to specify information.
Example
The following is an example to access attribute and methods in C# −
#define DEBUG
using System;
using System.Diagnostics;
public class Demo {
[Conditional("DEBUG")]
public static void Message(string str) {
Console.WriteLine(str);
}
}
class Test {
static void functionDisplay() {
Demo.Message("Our function...");
}
public static void Main() {
Demo.Message("This is Main function!");
functionDisplay();
Console.ReadKey();
}
}Output
This is Main function! Our function...