Attribute is just a way to add additional information (metadata) to the class, struct or some member. This meta-data could be retrieved by other code in order to make some decisions.
Simplest example is SerializableAttribute from the .NET. It indicates that the class could be serialized by a BinaryFormatter later on.
Here's another example - we could mark some classes in our code with ImmutableAttribute to indicate that they don't have any mutable fields and are OK for multi-threaded operations:
[Immutable]
public sealed class ProcessingMessage
{
//... some code that should be thread-safe
}
Then, we could create a unit test that finds all classes with the attribute and ensures that they are immutable indeed:
[Test]
public void Immutable_Types_Should_Be_Immutable()
{
var decorated = GlobalSetup.Types
.Where(t => t.Has<ImmutableAttribute>());
foreach (var type in decorated)
{
var count = type.GetAllFields().Count(f => !f.IsInitOnly && !f.IsStatic);
Assert.AreEqual(0, count, "Type {0} should be immutable", type);
}
}