Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

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:

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.

Related Sections

C# Language Specification

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

  • 1.12 Attributes

  • 17 Attributes

See Also

Community Additions

© 2015 Microsoft