User-Defined Attributes (UDA)

You will learn about the user-defined attributes (UDA) in this lesson.

We'll cover the following


Any declaration (e.g., struct type, class type, variable, etc.) can be assigned attributes, which can then be accessed at compile time to alter the way the code is compiled. User-defined attributes are purely a compile-time feature.

The user-defined attribute syntax consists of the @ sign followed by the attribute and appears before the declaration that it is being assigned to. For example, the following code assigns the Encrypted attribute to the declaration of name:

@Encrypted string name;

Multiple attributes can be specified separately or as a parenthesized list of attributes. For example, both of the following variables have the same attributes:

@Encrypted @Colored string lastName; // ← separately 
@(Encrypted, Colored) string address; // ← together

An attribute can be a type name as well as the value of a user-defined or a fundamental type. However, because their meanings may not be clear, attributes consisting of literal values like 42 are discouraged:

Get hands-on with 1000+ tech skills courses.