Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Visual C#
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
C# Programming Guide
Attributes (C# Programming Guide)

Updated: July 2008

Attributes provide a powerful method of associating declarative information with C# code (types, methods, properties, and so forth). After an attribute is associated with a program entity, the attribute can be queried at run time by using a technique called reflection. For more information, see Reflection (C# Programming Guide).

Attributes occur in two forms:

  • Attributes that are defined in the common language runtime (CLR).

  • Custom attributes that you can create, to add extra information to your code. This information can later be retrieved programmatically.

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

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

Attributes have the following properties:

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

  • 10.2.1 Attributes

  • 17 Attributes

Date

History

Reason

July 2008

Updated overview section.

Content bug fix.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
typo      weonlyhadzeros   |   Edit   |   Show History

Attributes such as MarshallAsare used extensively in COM interop scenarios. For more information, see System.Runtime.InteropServices..::.MarshalAsAttribute

should be

Attributes such as MarshallAs are used extensively in COM interop scenarios. For more information, see System.Runtime.InteropServices..::.MarshalAsAttribute

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker