- •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
1• Keep the following information for run-time instantiation of the attribute: the attribute class T, the
2instance constructor C on T, the positional-argument-list P and the named-argument-list N, with the
3values resolved completely at compile-time.
424.3.2 Run-time retrieval of an attribute instance
5Compilation of an attribute yields an attribute class T, an instance constructor C on T, a positional-argument-
6list P, and a named-argument-list N. Given this information, an attribute instance can be retrieved at run-time
7using the following steps:
8• Follow the run-time processing steps for executing an object-creation-expression of the form
9new T(P), using the instance constructor C and values as determined at compile-time. These steps
10either result in an exception, or produce an instance of T. Call this instance O.
11• For each named-argument Arg in N, in order:
12o Let Name be the identifier of the named-argument Arg. If Name does not identify a non-static public
13read-write field or property on O, then an exception is thrown.
14o Let Value be the result of evaluating the attribute-argument-expression of Arg.
15o If Name identifies a field on O, then set this field to the value Value.
16o Otherwise, Name identifies a property on O. Set this property to the value Value.
17o The result is O, an instance of the attribute class T that has been initialized with the positional-
18argument-list P and the named-argument-list N.
1924.4 Reserved attributes
20The following attributes affect the language, as stated:
21• System.AttributeUsageAttribute (§24.4.1), which is used to describe the ways in which an
22attribute class can be used.
23• System.ConditionalAttribute (§24.4.2), which is used to define conditional methods and
24attributes.
25• System.ObsoleteAttribute (§24.4.3), which is used to mark a member as obsolete.
2624.4.1 The AttributeUsage attribute
27The attribute AttributeUsage is used to describe the manner in which the attribute class can be used.
28A class that is decorated with the AttributeUsage attribute shall derive from System.Attribute, either
29directly or indirectly. Otherwise, a compile-time error occurs.
30[Note: For an example of using this attribute, see §24.1.1. end note]
3124.4.2 The Conditional attribute
32The attribute Conditional enables the definition of conditional methods and conditional attribute
33classes.
3424.4.2.1 Conditional Methods
35A method decorated with the Conditional attribute is said to be a conditional method.The Conditional
36attribute indicates a condition by testing a conditional compilation symbol. Calls to a conditional method are
37either included or omitted depending on whether this symbol is defined at the point of the call. If the symbol
38is defined, the call is included; otherwise, the call is omitted.
39A conditional method is subject to the following restrictions:
373
C# LANGUAGE SPECIFICATION
1• The conditional method shall be a method in a class-declaration or struct-declaration. A compile-time
2error occurs if the Conditional attribute is specified on an interface method.
3• The conditional method shall have a return type of void.
4• The conditional method shall not be marked with the override modifier. A conditional method can be
5marked with the virtual modifier, however. Overrides of such a method are implicitly conditional,
6and shall not be explicitly marked with a Conditional attribute.
7• The conditional method shall not be an implementation of an interface method. Otherwise, a compile-
8time error occurs.
9In addition, a compile-time error occurs if a conditional method is used in a delegate-creation-expression.
10[Example: The example
11#define DEBUG
12using System;
13using System.Diagnostics;
14class Class1
15{
16 |
[Conditional("DEBUG")] |
17 |
public static void M() { |
18 |
Console.WriteLine("Executed Class1.M"); |
19}
20}
21class Class2
22{
23 |
public static void Test() { |
24 |
Class1.M(); |
25}
26}
27declares Class1.M as a conditional method. Class2's Test method calls this method. Since the
28conditional compilation symbol DEBUG is defined, if Class2.Test is called, it will call M. If the symbol
29DEBUG had not been defined, then Class2.Test would not call Class1.M. end example]
30It is important to understand that the inclusion or exclusion of a call to a conditional method is controlled by
31the conditional compilation symbols at the point of the call. [Example: In the following code
32// Begin class1.cs
33 |
using System; |
34 |
using System.Diagnostics; |
35 |
class Class1 |
36 |
{ |
37 |
[Conditional("DEBUG")] |
38 |
public static void F() { |
39 |
Console.WriteLine("Executed Class1.F"); |
40 |
} |
41}
42// End class1.cs
43
44 |
// Begin class2.cs |
|
45 |
#define DEBUG |
|
46 |
class Class2 |
|
47 |
{ |
|
48 |
public static void G() { |
|
49 |
Class1.F(); |
// F is called |
50 |
} |
|
51}
52// End class2.cs
374
|
Chapter 24 Attributes |
|
1 |
|
|
2 |
// Begin class3.cs |
|
3 |
#undef DEBUG |
|
4 |
class Class3 |
|
5 |
{ |
|
6 |
public static void H() { |
|
7 |
Class1.F(); |
// F is not called |
8 |
} |
|
9}
10// End class3.cs
11the classes Class2 and Class3 each contain calls to the conditional method Class1.F, which is
12conditional based on whether or not DEBUG is defined. Since this symbol is defined in the context of Class2
13but not Class3, the call to F in Class2 is included, while the call to F in Class3 is omitted. end example]
14The use of conditional methods in an inheritance chain can be confusing. Calls made to a conditional
15method through base, of the form base.M, are subject to the normal conditional method call rules.
16[Example: In the following code
17// Begin class1.cs
18 |
using System; |
19 |
using System.Diagnostics; |
20 |
class Class1 |
21 |
{ |
22 |
[Conditional("DEBUG")] |
23 |
public virtual void M() { |
24 |
Console.WriteLine("Class1.M executed"); |
25 |
} |
26}
27// End class1.cs
28
29 |
// Begin class2.cs |
|
30 |
using System; |
|
31 |
class Class2: Class1 |
|
32 |
{ |
|
33 |
public override void M() { |
|
34 |
Console.WriteLine("Class2.M executed"); |
|
35 |
base.M(); |
// base.M is not called! |
36 |
} |
|
37}
38// End class2.cs
39
40 |
// Begin class3.cs |
|
41 |
#define DEBUG |
|
42 |
using System; |
|
43 |
class Class3 |
|
44 |
{ |
|
45 |
public static void Test() { |
|
46 |
Class2 c = new Class2(); |
|
47 |
c.M(); |
// M is called |
48 |
} |
|
49}
50// End class3.cs
51Class2 includes a call to the M defined in its base class. This call is omitted because the base method is
52conditional based on the presence of the symbol DEBUG, which is undefined. Thus, the method writes to the
375
