Share via


Attributes (C# Programming Guide) 

Attributes provide a powerful method of associating declarative information with C# code (types, methods, properties, and so forth). Once associated with a program entity, the attribute can be queried at run time using a technique called Reflection.

Attributes exist in two forms: attributes that are defined in the Common Language Runtime's base class library and custom attributes that you can create, to add extra information to your code. This information can later be retrieved programmatically.

In this example, the attribute System.Reflection.TypeAttributes.Serializable is used to apply a specific characteristic to a class:

[System.Serializable]
public class SampleClass
{
    // Objects of this type can be serialized.
}

Attribute Overview

Attributes have the following properties:

  • Attributes add metadata to your program. Metadata is information embedded in your program such as compiler instructions or descriptions of data.

  • Your program can examine its own metadata using Reflection. See Accessing Attributes With Reflection.

  • Attributes are commonly used when interacting with COM.

For more information, see:

C# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 1.12 Attributes

  • 17 Attributes

See Also

Concepts

C# Programming Guide
Reflection (C# Programming Guide)
Attributes Overview
Common Uses for Attributes