
- •Foreword
- •Introduction
- •Scope
- •Conformance
- •Normative references
- •Definitions
- •Notational conventions
- •Acronyms and abbreviations
- •General description
- •Language overview
- •Getting started
- •Types
- •Predefined types
- •Conversions
- •Array types
- •Type system unification
- •Variables and parameters
- •Automatic memory management
- •Expressions
- •Statements
- •Classes
- •Constants
- •Fields
- •Methods
- •Properties
- •Events
- •Operators
- •Indexers
- •Instance constructors
- •Destructors
- •Static constructors
- •Inheritance
- •Static classes
- •Partial type declarations
- •Structs
- •Interfaces
- •Delegates
- •Enums
- •Namespaces and assemblies
- •Versioning
- •Extern Aliases
- •Attributes
- •Generics
- •Why generics?
- •Creating and consuming generics
- •Multiple type parameters
- •Constraints
- •Generic methods
- •Anonymous methods
- •Iterators
- •Lexical structure
- •Programs
- •Grammars
- •Lexical grammar
- •Syntactic grammar
- •Grammar ambiguities
- •Lexical analysis
- •Line terminators
- •Comments
- •White space
- •Tokens
- •Unicode escape sequences
- •Identifiers
- •Keywords
- •Literals
- •Boolean literals
- •Integer literals
- •Real literals
- •Character literals
- •String literals
- •The null literal
- •Operators and punctuators
- •Pre-processing directives
- •Conditional compilation symbols
- •Pre-processing expressions
- •Declaration directives
- •Conditional compilation directives
- •Diagnostic directives
- •Region control
- •Line directives
- •Pragma directives
- •Basic concepts
- •Application startup
- •Application termination
- •Declarations
- •Members
- •Namespace members
- •Struct members
- •Enumeration members
- •Class members
- •Interface members
- •Array members
- •Delegate members
- •Member access
- •Declared accessibility
- •Accessibility domains
- •Protected access for instance members
- •Accessibility constraints
- •Signatures and overloading
- •Scopes
- •Name hiding
- •Hiding through nesting
- •Hiding through inheritance
- •Namespace and type names
- •Unqualified name
- •Fully qualified names
- •Automatic memory management
- •Execution order
- •Types
- •Value types
- •The System.ValueType type
- •Default constructors
- •Struct types
- •Simple types
- •Integral types
- •Floating point types
- •The decimal type
- •The bool type
- •Enumeration types
- •Reference types
- •Class types
- •The object type
- •The string type
- •Interface types
- •Array types
- •Delegate types
- •Boxing and unboxing
- •Boxing conversions
- •Unboxing conversions
- •Variables
- •Variable categories
- •Static variables
- •Instance variables
- •Instance variables in classes
- •Instance variables in structs
- •Array elements
- •Value parameters
- •Reference parameters
- •Output parameters
- •Local variables
- •Default values
- •Definite assignment
- •Initially assigned variables
- •Initially unassigned variables
- •Precise rules for determining definite assignment
- •General rules for statements
- •Block statements, checked, and unchecked statements
- •Expression statements
- •Declaration statements
- •If statements
- •Switch statements
- •While statements
- •Do statements
- •For statements
- •Break, continue, and goto statements
- •Throw statements
- •Return statements
- •Try-catch statements
- •Try-finally statements
- •Try-catch-finally statements
- •Foreach statements
- •Using statements
- •Lock statements
- •General rules for simple expressions
- •General rules for expressions with embedded expressions
- •Invocation expressions and object creation expressions
- •Simple assignment expressions
- •&& expressions
- •|| expressions
- •! expressions
- •?: expressions
- •Anonymous method expressions
- •Yield statements
- •Variable references
- •Atomicity of variable references
- •Conversions
- •Implicit conversions
- •Identity conversion
- •Implicit numeric conversions
- •Implicit enumeration conversions
- •Implicit reference conversions
- •Boxing conversions
- •Implicit type parameter conversions
- •Implicit constant expression conversions
- •User-defined implicit conversions
- •Explicit conversions
- •Explicit numeric conversions
- •Explicit enumeration conversions
- •Explicit reference conversions
- •Unboxing conversions
- •User-defined explicit conversions
- •Standard conversions
- •Standard implicit conversions
- •Standard explicit conversions
- •User-defined conversions
- •Permitted user-defined conversions
- •Evaluation of user-defined conversions
- •User-defined implicit conversions
- •User-defined explicit conversions
- •Anonymous method conversions
- •Method group conversions
- •Expressions
- •Expression classifications
- •Values of expressions
- •Operators
- •Operator precedence and associativity
- •Operator overloading
- •Unary operator overload resolution
- •Binary operator overload resolution
- •Candidate user-defined operators
- •Numeric promotions
- •Unary numeric promotions
- •Binary numeric promotions
- •Member lookup
- •Base types
- •Function members
- •Argument lists
- •Overload resolution
- •Applicable function member
- •Better function member
- •Better conversion
- •Function member invocation
- •Invocations on boxed instances
- •Primary expressions
- •Literals
- •Simple names
- •Invariant meaning in blocks
- •Parenthesized expressions
- •Member access
- •Identical simple names and type names
- •Invocation expressions
- •Method invocations
- •Delegate invocations
- •Element access
- •Array access
- •Indexer access
- •This access
- •Base access
- •Postfix increment and decrement operators
- •The new operator
- •Object creation expressions
- •Array creation expressions
- •Delegate creation expressions
- •The typeof operator
- •The checked and unchecked operators
- •Default value expression
- •Anonymous methods
- •Anonymous method signatures
- •Anonymous method blocks
- •Outer variables
- •Captured outer variables
- •Instantiation of local variables
- •Anonymous method evaluation
- •Implementation example
- •Unary expressions
- •Unary plus operator
- •Unary minus operator
- •Logical negation operator
- •Bitwise complement operator
- •Prefix increment and decrement operators
- •Cast expressions
- •Arithmetic operators
- •Multiplication operator
- •Division operator
- •Remainder operator
- •Addition operator
- •Subtraction operator
- •Shift operators
- •Relational and type-testing operators
- •Integer comparison operators
- •Floating-point comparison operators
- •Decimal comparison operators
- •Boolean equality operators
- •Enumeration comparison operators
- •Reference type equality operators
- •String equality operators
- •Delegate equality operators
- •The is operator
- •The as operator
- •Logical operators
- •Integer logical operators
- •Enumeration logical operators
- •Boolean logical operators
- •Conditional logical operators
- •Boolean conditional logical operators
- •User-defined conditional logical operators
- •Conditional operator
- •Assignment operators
- •Simple assignment
- •Compound assignment
- •Event assignment
- •Expression
- •Constant expressions
- •Boolean expressions
- •Statements
- •End points and reachability
- •Blocks
- •Statement lists
- •The empty statement
- •Labeled statements
- •Declaration statements
- •Local variable declarations
- •Local constant declarations
- •Expression statements
- •Selection statements
- •The if statement
- •The switch statement
- •Iteration statements
- •The while statement
- •The do statement
- •The for statement
- •The foreach statement
- •Jump statements
- •The break statement
- •The continue statement
- •The goto statement
- •The return statement
- •The throw statement
- •The try statement
- •The checked and unchecked statements
- •The lock statement
- •The using statement
- •The yield statement
- •Namespaces
- •Compilation units
- •Namespace declarations
- •Extern alias directives
- •Using directives
- •Using alias directives
- •Using namespace directives
- •Namespace members
- •Type declarations
- •Qualified alias member
- •Classes
- •Class declarations
- •Class modifiers
- •Abstract classes
- •Sealed classes
- •Static classes
- •Class base specification
- •Base classes
- •Interface implementations
- •Class body
- •Partial declarations
- •Class members
- •Inheritance
- •The new modifier
- •Access modifiers
- •Constituent types
- •Static and instance members
- •Nested types
- •Fully qualified name
- •Declared accessibility
- •Hiding
- •this access
- •Reserved member names
- •Member names reserved for properties
- •Member names reserved for events
- •Member names reserved for indexers
- •Member names reserved for destructors
- •Constants
- •Fields
- •Static and instance fields
- •Readonly fields
- •Using static readonly fields for constants
- •Versioning of constants and static readonly fields
- •Volatile fields
- •Field initialization
- •Variable initializers
- •Static field initialization
- •Instance field initialization
- •Methods
- •Method parameters
- •Value parameters
- •Reference parameters
- •Output parameters
- •Parameter arrays
- •Static and instance methods
- •Virtual methods
- •Override methods
- •Sealed methods
- •Abstract methods
- •External methods
- •Method body
- •Method overloading
- •Properties
- •Static and instance properties
- •Accessors
- •Virtual, sealed, override, and abstract accessors
- •Events
- •Field-like events
- •Event accessors
- •Static and instance events
- •Virtual, sealed, override, and abstract accessors
- •Indexers
- •Indexer overloading
- •Operators
- •Unary operators
- •Binary operators
- •Conversion operators
- •Instance constructors
- •Constructor initializers
- •Instance variable initializers
- •Constructor execution
- •Default constructors
- •Private constructors
- •Optional instance constructor parameters
- •Static constructors
- •Destructors
- •Structs
- •Struct declarations
- •Struct modifiers
- •Struct interfaces
- •Struct body
- •Struct members
- •Class and struct differences
- •Value semantics
- •Inheritance
- •Assignment
- •Default values
- •Boxing and unboxing
- •Meaning of this
- •Field initializers
- •Constructors
- •Destructors
- •Static constructors
- •Struct examples
- •Database integer type
- •Database boolean type
- •Arrays
- •Array types
- •The System.Array type
- •Array creation
- •Array element access
- •Array members
- •Array covariance
- •Arrays and the generic IList interface
- •Array initializers
- •Interfaces
- •Interface declarations
- •Interface modifiers
- •Base interfaces
- •Interface body
- •Interface members
- •Interface methods
- •Interface properties
- •Interface events
- •Interface indexers
- •Interface member access
- •Fully qualified interface member names
- •Interface implementations
- •Explicit interface member implementations
- •Interface mapping
- •Interface implementation inheritance
- •Interface re-implementation
- •Abstract classes and interfaces
- •Enums
- •Enum declarations
- •Enum modifiers
- •Enum members
- •The System.Enum type
- •Enum values and operations
- •Delegates
- •Delegate declarations
- •Delegate instantiation
- •Delegate invocation
- •Exceptions
- •Causes of exceptions
- •The System.Exception class
- •How exceptions are handled
- •Common Exception Classes
- •Attributes
- •Attribute classes
- •Attribute usage
- •Positional and named parameters
- •Attribute parameter types
- •Attribute specification
- •Attribute instances
- •Compilation of an attribute
- •Run-time retrieval of an attribute instance
- •Reserved attributes
- •The AttributeUsage attribute
- •The Conditional attribute
- •Conditional Methods
- •Conditional Attribute Classes
- •The Obsolete attribute
- •Unsafe code
- •Unsafe contexts
- •Pointer types
- •Fixed and moveable variables
- •Pointer conversions
- •Pointers in expressions
- •Pointer indirection
- •Pointer member access
- •Pointer element access
- •The address-of operator
- •Pointer increment and decrement
- •Pointer arithmetic
- •Pointer comparison
- •The sizeof operator
- •The fixed statement
- •Stack allocation
- •Dynamic memory allocation
- •Generics
- •Generic class declarations
- •Type parameters
- •The instance type
- •Members of generic classes
- •Static fields in generic classes
- •Static constructors in generic classes
- •Accessing protected members
- •Overloading in generic classes
- •Parameter array methods and type parameters
- •Overriding and generic classes
- •Operators in generic classes
- •Nested types in generic classes
- •Generic struct declarations
- •Generic interface declarations
- •Uniqueness of implemented interfaces
- •Explicit interface member implementations
- •Generic delegate declarations
- •Constructed types
- •Type arguments
- •Open and closed types
- •Base classes and interfaces of a constructed type
- •Members of a constructed type
- •Accessibility of a constructed type
- •Conversions
- •Using alias directives
- •Generic methods
- •Generic method signatures
- •Virtual generic methods
- •Calling generic methods
- •Inference of type arguments
- •Using a generic method with a delegate
- •Constraints
- •Satisfying constraints
- •Member lookup on type parameters
- •Type parameters and boxing
- •Conversions involving type parameters
- •Iterators
- •Iterator blocks
- •Enumerator interfaces
- •Enumerable interfaces
- •Yield type
- •This access
- •Enumerator objects
- •The MoveNext method
- •The Current property
- •The Dispose method
- •Enumerable objects
- •The GetEnumerator method
- •Implementation example
- •Lexical grammar
- •Line terminators
- •White space
- •Comments
- •Unicode character escape sequences
- •Identifiers
- •Keywords
- •Literals
- •Operators and punctuators
- •Pre-processing directives
- •Syntactic grammar
- •Basic concepts
- •Types
- •Expressions
- •Statements
- •Classes
- •Structs
- •Arrays
- •Interfaces
- •Enums
- •Delegates
- •Attributes
- •Generics
- •Grammar extensions for unsafe code
- •Undefined behavior
- •Implementation-defined behavior
- •Unspecified behavior
- •Other Issues
- •Capitalization styles
- •Pascal casing
- •Camel casing
- •All uppercase
- •Capitalization summary
- •Word choice
- •Namespaces
- •Classes
- •Interfaces
- •Enums
- •Static fields
- •Parameters
- •Methods
- •Properties
- •Events
- •Case sensitivity
- •Avoiding type name confusion
- •Documentation Comments
- •Introduction
- •Recommended tags
- •<code>
- •<example>
- •<exception>
- •<list>
- •<para>
- •<param>
- •<paramref>
- •<permission>
- •<remarks>
- •<returns>
- •<seealso>
- •<summary>
- •<value>
- •Processing the documentation file
- •ID string format
- •ID string examples
- •An example
- •C# source code
- •Resulting XML
Chapter 24 Attributes
1defines an attribute class named HelpAttribute that has one positional parameter (string url) and one
2named parameter (string Topic). Although it is non-static and public, the property Url does not define a
3named parameter, since it is not read-write.
4This attribute class might be used as follows:
5[Help("http://www.mycompany.com/…/Class1.htm")]
6class Class1
7{
8}
9[Help("http://www.mycompany.com/…/Misc.htm", Topic ="Class2")]
10class Class2
11{
12}
13end example]
1424.1.3 Attribute parameter types
15The types of positional and named parameters for an attribute class are limited to the attribute parameter
16types, which are:
17• One of the following types: bool, byte, char, double, float, int, long, short, string.
18• The type object.
19• The type System.Type.
20• An enum type, provided it has public accessibility and the types in which it is nested (if any) also have
21public accessibility.
22• Single-dimensional arrays of the above types.
2324.2 Attribute specification
24Attribute specification is the application of a previously defined attribute to a declaration. An attribute is a
25piece of additional declarative information that is specified for a declaration. Attributes can be specified at
26global scope (to specify attributes on the containing assembly) and for type-declarations (§16.6), class-
27member-declarations (§17.1.4), struct-member-declarations (§18.2), interface-member-declarations
28(§20.2), enum-member-declarations (§21.1), accessor-declarations (§17.6.2), event-accessor-declarations
29(§17.7), elements of formal-parameter-lists (§17.5.1), and elements of type-parameter-lists (§26.1.1).
30Attributes are specified in attribute sections. An attribute section consists of a pair of square brackets, which
31surround a comma-separated list of one or more attributes. The order in which attributes are specified in
32such a list, and the order in which sections attached to the same program entity are arranged, is not
33significant. For instance, the attribute specifications [A][B], [B][A], [A, B], and [B, A] are equivalent.
34global-attributes:
35 |
global-attribute-sections |
36 |
global-attribute-sections: |
37 |
global-attribute-section |
38 |
global-attribute-sections global-attribute-section |
39 |
global-attribute-section: |
40 |
[ global-attribute-target-specifier attribute-list ] |
41 |
[ global-attribute-target-specifier attribute-list , ] |
42 |
global-attribute-target-specifier: |
43 |
global-attribute-target : |
44 |
global-attribute-target: |
45 |
identifier-or-keyword |
367
|
C# LANGUAGE SPECIFICATION |
|
1 |
attributes: |
|
2 |
attribute-sections |
|
3 |
attribute-sections: |
|
4 |
attribute-section |
|
5 |
attribute-sections attribute-section |
|
6 |
attribute-section: |
|
7 |
[ attribute-target-specifieropt |
attribute-list ] |
8 |
[ attribute-target-specifieropt attribute-list , ] |
|
9 |
attribute-target-specifier: |
|
10 |
attribute-target : |
|
11 |
attribute-target: |
|
12 |
identifier-or-keyword |
|
13 |
attribute-list: |
|
14 |
attribute |
|
15 |
attribute-list , attribute |
|
16 |
attribute: |
|
17 |
attribute-name attribute-argumentsopt |
|
18 |
attribute-name: |
|
19 |
type-name |
|
20 |
attribute-arguments: |
|
21 |
( positional-argument-listopt |
) |
22 |
( positional-argument-list , |
named-argument-list ) |
23( named-argument-list )
24positional-argument-list:
25 |
positional-argument |
26 |
positional-argument-list , positional-argument |
27 |
positional-argument: |
28 |
attribute-argument-expression |
29 |
named-argument-list: |
30 |
named-argument |
31 |
named-argument-list , named-argument |
32 |
named-argument: |
33 |
identifier = attribute-argument-expression |
34 |
attribute-argument-expression: |
35 |
expression |
36An attribute consists of an attribute-name and an optional list of positional and named arguments. The
37positional arguments (if any) precede the named arguments. A positional argument consists of an attribute-
38argument-expression; a named argument consists of a name, followed by an equal sign, followed by an
39attribute-argument-expression, which, together, are constrained by the same rules as simple assignment. The
40order of named arguments is not significant.
41[Note: For convenience, a trailing comma is allowed in a global-attribute-section and an attribute-section,
42just as one is allowed in an array-initializer (§19.6). end note]
43The attribute-name identifies an attribute class. type-name shall refer to an attribute class. Otherwise, a
44compile-time error occurs. [Example: The example
45class Class1 {}
46 |
[Class1] class Class2 {} // Error |
368
Chapter 24 Attributes
1results in a compile-time error because it attempts to use Class1 as an attribute class when Class1 is not
2an attribute class. end example]
3When an attribute is placed at the global level, a global-attribute-target-specifier is required. The only
4standardized global-attribute-target name is assembly. This target name shall only be used in the context
5of an assembly.
6The only standardized attribute-target names are event, field, method, param, property, return, and
7type. These target names shall only be used in the following contexts:
8• event — an event.
9• field — a field. A field-like event (i.e., one without accessors) can also have an attribute with this
10target.
11• method — a constructor, destructor, method, operator, property get and set accessors, and event add and
12remove accessors. A field-like event (i.e., one without accessors) can also have an attribute with this
13target.
14• param — a property set accessor, event add and remove accessors, and a parameter in a constructor,
15method, and operator.
16• property — a property.
17• return — a delegate, method, operator, and property get accessor.
18• type — a delegate, class, struct, and interface.
19• typevar — a type parameter.
20Certain contexts permit the specification of an attribute on more than one target. A program can explicitly
21specify the target by including an attribute-target-specifier. In the absence of an attribute-target-specifier, a
22reasonable default is applied, but an attribute-target-specifier can be used to affirm or override the default in
23certain ambiguous cases (or to just affirm the default in non-ambiguous cases). Thus, typically, attribute-
24target-specifiers can be omitted. The potentially ambiguous contexts are resolved as follows (using equality
25as defined in §9.4.2):
26• An attribute specified on a delegate declaration can apply either to the delegate being declared or to its
27return value. In the absence of an attribute-target-specifier, the attribute applies to the delegate. An
28attribute-target equal to type indicates that the attribute applies to the delegate; an attribute-target
29equal to return indicates that the attribute applies to the return value.
30• An attribute specified on a method declaration can apply either to the method being declared or to its
31return value. In the absence of an attribute-target-specifier, the attribute applies to the method. An
32attribute-target equal to method indicates that the attribute applies to the method; an attribute-target
33equal to return indicates that the attribute applies to the return value.
34• An attribute specified on an operator declaration can apply either to the operator being declared or to its
35return value. In the absence of an attribute-target-specifier, the attribute applies to the operator. An
36attribute-target equal to method indicates that the attribute applies to the operator; an attribute-target
37equal to return indicates that the attribute applies to the return value.
38• An attribute specified on a get accessor declaration for a property or indexer declaration can apply either
39to the associated method or to its return value. In the absence of an attribute-target-specifier, the
40attribute applies to the method. An attribute-target equal to method indicates that the attribute applies to
41the method; an attribute-target equal to return indicates that the attribute applies to the return value.
42• An attribute specified on a set accessor for a property or indexer declaration can apply either to the
43associated method or to its lone implicit parameter. In the absence of an attribute-target-specifier, the
44attribute applies to the method. An attribute-target equal to method indicates that the attribute applies to
45the method; the presence of an attribute-target equal to param indicates that the attribute applies to the
46parameter.
369
C# LANGUAGE SPECIFICATION
1• An attribute specified on an event declaration that omits event-accessor-declarations can apply to the
2event being declared, to the associated field (if the event is not abstract), or to the associated add and
3remove methods. In the absence of an attribute-target-specifier, the attribute applies to the event
4declaration. An attribute-target equal to event indicates that the attribute applies to the event; an
5attribute-target equal to field indicates that the attribute applies to the field; and an attribute-target
6equal to method indicates that the attribute applies to the methods.
7• In the case of an event declaration that does not omit event-accessor-declarations, an attribute specified
8on an add or remove accessor declaration for an event declaration can apply either to the associated
9method or to its lone parameter. In the absence of an attribute-target-specifier, the attribute applies to
10the method. An attribute-target equal to method indicates that the attribute applies to the method; an
11attribute-target equal to param indicates that the attribute applies to the parameter.
12An implementation can accept other attribute target specifiers, the purposes of which are implementation-
13defined. However, an implementation that does not recognize such a target, shall issue a warning.
14By convention, attribute classes are named with a suffix of Attribute. An attribute-name can either
15include or omit this suffix. Specifically, an attribute-name is resolved as follows:
16• If the right-most identifier of the attribute-name is a verbatim identifier (§9.4.2), then the attribute-name
17is resolved as a type-name (§10.8). If the result is not a type derived from System.Attribute, a
18compile-time error occurs.
19• Otherwise,
20o The attribute-name is resolved as a type-name (§10.8) except any errors are suppressed. If this
21resolution is successful and results in a type derived from System.Attribute then the type is the
22result of this step.
23o The characters Attribute are appended to the right-most identifier in the attribute-name and the
24resulting string of tokens is resolved as a type-name (§10.8) except any errors are suppressed. If this
25resolution is successful and results in a type derived from System.Attribute then the type is the
26result of this step.
27If exactly one of the two steps above results in a type derived from System.Attribute, then that type
28is the result of the attribute-name. Otherwise a compile-time error occurs.
29[Example: Informally, when attempting to resolve an attribute-name, if an attribute class is found both with
30and without the Attribute suffix, an ambiguity is present, and a compile-time error is issued. If the
31attribute-name is spelled such that its right-most identifier is a verbatim identifier (§9.4.2), then only an
32attribute without a suffix is matched, thus enabling such an ambiguity to be resolved. The example
33using System;
34[AttributeUsage(AttributeTargets.All)]
35public class X: Attribute
36{}
37[AttributeUsage(AttributeTargets.All)]
38public class XAttribute: Attribute
39{}
40 |
[X] |
// error: ambiguity |
41 |
class Class1 {} |
|
42 |
[XAttribute] |
// refers to XAttribute |
43 |
class Class2 {} |
|
44 |
[@X] |
// refers to X |
45 |
class Class3 {} |
|
46 |
[@XAttribute] |
// refers to XAttribute |
47class Class4 {}
48shows two attribute classes named X and XAttribute. The attribute reference [X] is ambiguous, since it
49could refer to either X or XAttribute. Using a verbatim identifier allows the exact intent to be specified in
50such rare cases. The attribute reference [XAttribute] is not ambiguous (although it would be if there were
370
Chapter 24 Attributes
1an attribute class named XAttributeAttribute!). If the declaration for class X is removed, then both
2attributes refer to the attribute class named XAttribute, as follows:
3using System;
4[AttributeUsage(AttributeTargets.All)]
5public class XAttribute: Attribute
6{}
7 |
[X] |
// refers to XAttribute |
8 |
class Class1 {} |
|
9 |
[XAttribute] |
// refers to XAttribute |
10 |
class Class2 {} |
|
11 |
[@X] |
// error: no attribute named “X” |
12class Class3 {}
13end example]
14It is a compile-time error to use a single-use attribute class more than once on the same entity. [Example:
15The example
16using System;
17[AttributeUsage(AttributeTargets.Class)]
18public class HelpStringAttribute: Attribute
19{
20 |
string value; |
21 |
public HelpStringAttribute(string value) { |
22 |
this.value = value; |
23 |
} |
24public string Value { get {…} }
25}
26[HelpString("Description of Class1")]
27[HelpString("Another description of Class1")]
28public class Class1 {}
29results in a compile-time error because it attempts to use HelpString, which is a single-use attribute class,
30more than once on the declaration of Class1. end example]
31An expression E is an attribute-argument-expression if all of the following statements are true:
32• The type of E is an attribute parameter type (§24.1.3).
33• At compile-time, the value of E can be resolved to one of the following:
34o A constant value.
35o Atypeof-expression (§14.5.11) specifying a non-generic type, a closed constructed type (§26.5.2), or
36an unbound generic type (§26.5).
37o A one-dimensional array of attribute-argument-expressions.
38[Example:
39using System;
40[AttributeUsage(AttributeTargets.Class)]
41public class MyAttribute: Attribute
42{
43 |
public |
int P1 { |
44 |
get {…} |
|
45 |
set {…} |
|
46 |
} |
|
47 |
public Type P2 { |
|
48 |
get {…} |
|
49 |
set {…} |
|
50 |
} |
|
371