- •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
C# LANGUAGE SPECIFICATION
1A cast-expression of the form (T)E, where T is a type and E is a unary-expression, performs an explicit
2conversion (§13.2) of the value of E to type T. If no explicit conversion exists from the type of E to T, a
3compile-time error occurs. Otherwise, the result is the value produced by the explicit conversion. The result
4is always classified as a value, even if E denotes a variable.
5The grammar for a cast-expression leads to certain syntactic ambiguities. [Example: The expression
6(x)–y could either be interpreted as a cast-expression (a cast of –y to type x) or as an additive-
7 expression combined with a parenthesized-expression (which computes the value x – y). end example]
8To resolve cast-expression ambiguities, the following rule exists: A sequence of one or more tokens (§9.4)
9enclosed in parentheses is considered the start of a cast-expression only if at least one of the following are
10true:
11• The sequence of tokens is correct grammar for a type, but not for an expression.
12• The sequence of tokens is correct grammar for a type, and the token immediately following the closing
13parentheses is the token “~”, the token “!”, the token “(”, an identifier (§9.4.1), a literal (§9.4.4), or any
14keyword (§9.4.3) except as and is.
15The term “correct grammar” above means only that the sequence of tokens shall conform to the
16particular grammatical production. It specifically does not consider the actual meaning of any
17constituent identifiers. [Example: If x and y are identifiers, then x.y is correct grammar for a type, even
18if x.y doesn’t actually denote a type. end example]
19[Note: From the disambiguation rule, it follows that, if x and y are identifiers, (x)y, (x)(y), and
20(x)(-y) are cast-expressions, but (x)-y is not, even if x identifies a type. However, if x is a keyword
21that identifies a predefined type (such as int), then all four forms are cast-expressions (because such a
22keyword could not possibly be an expression by itself). end note]
2314.7 Arithmetic operators
24The *, /, %, +, and – operators are called the arithmetic operators.
25multiplicative-expression:
26 |
unary-expression |
27 |
multiplicative-expression * unary-expression |
28 |
multiplicative-expression / unary-expression |
29 |
multiplicative-expression % unary-expression |
30 |
additive-expression: |
31 |
multiplicative-expression |
32 |
additive-expression + multiplicative-expression |
33 |
additive-expression – multiplicative-expression |
3414.7.1 Multiplication operator
35For an operation of the form x * y, binary operator overload resolution (§14.2.4) is applied to select a
36specific operator implementation. The operands are converted to the parameter types of the selected
37operator, and the type of the result is the return type of the operator.
38The predefined multiplication operators are listed below. The operators all compute the product of x and y.
39• Integer multiplication:
40int operator *(int x, int y);
41uint operator *(uint x, uint y);
42long operator *(long x, long y);
43ulong operator *(ulong x, ulong y);
44void operator *(long x, ulong y);
45void operator *(ulong x, long y);
46The operators with void return type always produce a compile-time error. Consequently, it is an error
47for one operand to be of type long and the other to be of type ulong.
190
Chapter 14 Expressions
1In a checked context, if the product is outside the range of the result type, a
2System.OverflowException is thrown. In an unchecked context, overflows are not reported and
3any significant high-order bits outside the range of the result type are discarded.
4• Floating-point multiplication:
5float operator *(float x, float y);
6double operator *(double x, double y);
7The product is computed according to the rules of IEC 60559 arithmetic. The following table lists the
8results of all possible combinations of nonzero finite values, zeros, infinities, and NaN’s. In the table, x
9and y are positive finite values. z is the result of x * y, rounded to the nearest representable value. If
10the magnitude of the result is too large for the destination type, z is infinity. Because of rounding, z may
11be zero even though neither x nor y is zero.
12
|
+y |
–y |
+0 |
–0 |
+∞ |
–∞ |
NaN |
|
|
|
|
|
|
|
|
+x |
+z |
–z |
+0 |
–0 |
+∞ |
–∞ |
NaN |
|
|
|
|
|
|
|
|
–x |
–z |
+z |
–0 |
+0 |
–∞ |
+∞ |
NaN |
|
|
|
|
|
|
|
|
+0 |
+0 |
–0 |
+0 |
–0 |
NaN |
NaN |
NaN |
|
|
|
|
|
|
|
|
–0 |
–0 |
+0 |
–0 |
+0 |
NaN |
NaN |
NaN |
|
|
|
|
|
|
|
|
+∞ |
+∞ |
–∞ |
NaN |
NaN |
+∞ |
–∞ |
NaN |
|
|
|
|
|
|
|
|
–∞ |
–∞ |
+∞ |
NaN |
NaN |
–∞ |
+∞ |
NaN |
|
|
|
|
|
|
|
|
NaN |
NaN |
NaN |
NaN |
NaN |
NaN |
NaN |
NaN |
|
|
|
|
|
|
|
|
13
14• Decimal multiplication:
15decimal operator *(decimal x, decimal y);
16If the magnitude of the resulting value is too large to represent in the decimal format, a
17System.OverflowException is thrown. Because of rounding, the result may be zero even though
18neither operand is zero. The scale of the result, before any rounding, is the sum of the scales of the two
19operands.
20Decimal multiplication is equivalent to using the multiplication operator of type System.Decimal.
2114.7.2 Division operator
22For an operation of the form x / y, binary operator overload resolution (§14.2.4) is applied to select a
23specific operator implementation. The operands are converted to the parameter types of the selected
24operator, and the type of the result is the return type of the operator.
25The predefined division operators are listed below. The operators all compute the quotient of x and y.
26• Integer division:
27int operator /(int x, int y);
28uint operator /(uint x, uint y);
29long operator /(long x, long y);
30ulong operator /(ulong x, ulong y);
31void operator /(long x, ulong y);
32void operator /(ulong x, long y);
33The operators with void return type always produce a compile-time error. Consequently, it is an error
34for one operand to be of type long and the other to be of type ulong.
35If the value of the right operand is zero, a System.DivideByZeroException is thrown.
36The division rounds the result towards zero, and the absolute value of the result is the largest possible
37integer that is less than the absolute value of the quotient of the two operands. The result is zero or
38positive when the two operands have the same sign and zero or negative when the two operands have
39opposite signs.
191
C# LANGUAGE SPECIFICATION
1If the left operand is the smallest int or long value (−231 or −263, respectively) and the right operand is
2–1, an overflow occurs. In a checked context, this causes a System.ArithmeticException (or a
3subclass thereof) to be thrown. In an unchecked context, it is implementation-defined as to whether a
4System.ArithmeticException (or a subclass thereof) is thrown or the overflow goes unreported
5with the resulting value being that of the left operand.
6• Floating-point division:
7float operator /(float x, float y);
8double operator /(double x, double y);
9The quotient is computed according to the rules of IEC 60559 arithmetic. The following table lists the
10results of all possible combinations of nonzero finite values, zeros, infinities, and NaN’s. In the table, x
11and y are positive finite values. z is the result of x / y, rounded to the nearest representable value. If
12the magnitude of the result is too large for the destination type, z is infinity. Because of rounding, z may
13still be zero even though x is not zero and y is not infinite.
14
|
+y |
–y |
+0 |
–0 |
+∞ |
–∞ |
NaN |
|
|
|
|
|
|
|
|
+x |
+z |
–z |
+∞ |
–∞ |
+0 |
–0 |
NaN |
|
|
|
|
|
|
|
|
–x |
–z |
+z |
–∞ |
+∞ |
–0 |
+0 |
NaN |
|
|
|
|
|
|
|
|
+0 |
+0 |
–0 |
NaN |
NaN |
+0 |
–0 |
NaN |
|
|
|
|
|
|
|
|
–0 |
–0 |
+0 |
NaN |
NaN |
–0 |
+0 |
NaN |
|
|
|
|
|
|
|
|
+∞ |
+∞ |
–∞ |
+∞ |
–∞ |
NaN |
NaN |
NaN |
|
|
|
|
|
|
|
|
–∞ |
–∞ |
+∞ |
–∞ |
+∞ |
NaN |
NaN |
NaN |
|
|
|
|
|
|
|
|
NaN |
NaN |
NaN |
NaN |
NaN |
NaN |
NaN |
NaN |
|
|
|
|
|
|
|
|
15
16• Decimal division:
17decimal operator /(decimal x, decimal y);
18If the value of the right operand is zero, a System.DivideByZeroException is thrown. If the
19magnituded of the resulting value is too large to represent in the decimal format, a
20System.OverflowException is thrown. Because of rounding, the result may be zero event though
21the first operand is not zero. The scale of the result, before any rounding, is the closest scale to the
22preferred scale which will preserve a result equal to the exact result. The preferred scale is the scale of x
23less the scale of y.
24Decimal division is equivalent to using the division operator of type System.Decimal.
2514.7.3 Remainder operator
26For an operation of the form x % y, binary operator overload resolution (§14.2.4) is applied to select a
27specific operator implementation. The operands are converted to the parameter types of the selected
28operator, and the type of the result is the return type of the operator.
29The predefined remainder operators are listed below. The operators all compute the remainder of the
30division between x and y.
31• Integer remainder:
32int operator %(int x, int y);
33uint operator %(uint x, uint y);
34long operator %(long x, long y);
35ulong operator %(ulong x, ulong y);
36void operator %(long x, ulong y);
37void operator %(ulong x, long y);
38The operators with void return type always produce a compile-time error. Consequently, it is an error
39for one operand to be of type long and the other to be of type ulong.
192
Chapter 14 Expressions
1 The result of x % y is the value produced by x – (x / y) * y. If y is zero, a
2System.DivideByZeroException is thrown.
3If the left operand is the smallest int or long value (−231 or −263, respectively) and the right operand is
4–1, it is implementation-defined as to whether a System.ArithmeticException (or a subclass
5 |
thereof) is thrown. A conforming implementation shall not throw an exception for x % y in any case |
6 |
where x / y does not throw an exception. |
7• Floating-point remainder:
8float operator %(float x, float y);
9double operator %(double x, double y);
10The following table lists the results of all possible combinations of nonzero finite values, zeros,
11infinities, and NaN’s. In the table, x and y are positive finite values. z is the result of x % y and is
12computed as x – n * y, rounded to the nearest representable value, where n is the largest integer that is
13less than or equal to x / y. This method of computing the remainder is analogous to that used for
14integer operands, but differs from the IEC 60559 definition (in which n is the integer closest to x / y).
15
|
+y |
–y |
+0 |
–0 |
+∞ |
–∞ |
NaN |
|
|
|
|
|
|
|
|
+x |
+z |
+z |
NaN |
NaN |
+x |
+x |
NaN |
|
|
|
|
|
|
|
|
–x |
–z |
–z |
NaN |
NaN |
–x |
–x |
NaN |
|
|
|
|
|
|
|
|
+0 |
+0 |
+0 |
NaN |
NaN |
+0 |
+0 |
NaN |
|
|
|
|
|
|
|
|
–0 |
–0 |
–0 |
NaN |
NaN |
–0 |
–0 |
NaN |
|
|
|
|
|
|
|
|
+∞ |
NaN |
NaN |
NaN |
NaN |
NaN |
NaN |
NaN |
|
|
|
|
|
|
|
|
–∞ |
NaN |
NaN |
NaN |
NaN |
NaN |
NaN |
NaN |
|
|
|
|
|
|
|
|
NaN |
NaN |
NaN |
NaN |
NaN |
NaN |
NaN |
NaN |
|
|
|
|
|
|
|
|
16
17• Decimal remainder:
18decimal operator %(decimal x, decimal y);
19If the value of the right operand is zero, a System.DivideByZeroException is thrown. It is
20implementation-defined when a System.ArithmeticException (or a subclass thereof) is thrown. A
21 conforming implementation shall not throw an exception for x % y in any case where x / y does not
22throw an exception. The scale of the result, before any rounding, is the larger of the scales of the two
23operands, and the sign of the result, if non-zero, is the same as that of x.
24Decimal remainder is equivalent to using the remainder operator of type System.Decimal.
2514.7.4 Addition operator
26For an operation of the form x + y, binary operator overload resolution (§14.2.4) is applied to select a
27specific operator implementation. The operands are converted to the parameter types of the selected
28operator, and the type of the result is the return type of the operator.
29The predefined addition operators are listed below. For numeric and enumeration types, the predefined
30addition operators compute the sum of the two operands. When one or both operands are of type string,
31the predefined addition operators concatenate the string representation of the operands.
32• Integer addition:
33int operator +(int x, int y);
34uint operator +(uint x, uint y);
35long operator +(long x, long y);
36ulong operator +(ulong x, ulong y);
37void operator +(long x, ulong y);
38void operator +(ulong x, long y);
193
C# LANGUAGE SPECIFICATION
1The operators with void return type always produce a compile-time error. Consequently, it is an error
2for one operand to be of type long and the other to be of type ulong.
3In a checked context, if the sum is outside the range of the result type, a
4System.OverflowException is thrown. In an unchecked context, overflows are not reported and
5any significant high-order bits outside the range of the result type are discarded.
6• Floating-point addition:
7float operator +(float x, float y);
8double operator +(double x, double y);
9The sum is computed according to the rules of IEC 60559 arithmetic. The following table lists the
10results of all possible combinations of nonzero finite values, zeros, infinities, and NaN’s. In the table, x
11and y are nonzero finite values, and z is the result of x + y, rounded to the nearest representable value.
12If x and y have the same magnitude but opposite signs, z is positive zero. If the magnitude of x + y is
13too large to represent in the destination type, z is an infinity with the same sign as x + y.
14
|
y |
+0 |
–0 |
+∞ |
–∞ |
NaN |
|
|
|
|
|
|
|
x |
z |
x |
x |
+∞ |
–∞ |
NaN |
|
|
|
|
|
|
|
+0 |
y |
+0 |
+0 |
+∞ |
–∞ |
NaN |
|
|
|
|
|
|
|
–0 |
y |
+0 |
–0 |
+∞ |
–∞ |
NaN |
|
|
|
|
|
|
|
+∞ |
+∞ |
+∞ |
+∞ |
+∞ |
NaN |
NaN |
|
|
|
|
|
|
|
–∞ |
–∞ |
–∞ |
–∞ |
NaN |
–∞ |
NaN |
|
|
|
|
|
|
|
NaN |
NaN |
NaN |
NaN |
NaN |
NaN |
NaN |
|
|
|
|
|
|
|
15
16• Decimal addition:
17decimal operator +(decimal x, decimal y);
18If the magnitude of the resulting value is too large to represent in the decimal format, a
19System.OverflowException is thrown. The scale of the result, before any rounding, is the larger of
20the scales of the two operands.
21Decimal addition is equivalent to using the addition operator of type System.Decimal.
22• Enumeration addition. Every enumeration type implicitly provides the following predefined operators,
23where E is the enum type, and U is the underlying type of E:
24E operator +(E x, U y);
25E operator +(U x, E y);
26The operators are evaluated exactly as (E)((U)x + (U)y). These operators are only considered by
27overload resolution (§14.2.4) when one of the actual operands is of type E.
28• String concatenation:
29string operator +(string x, string y);
30string operator +(string x, object y);
31string operator +(object x, string y);
32The binary + operator performs string concatenation when one or both operands are of type string. If
33an operand of string concatenation is null, an empty string is substituted. Otherwise, any non-string
34operand is converted to its string representation by invoking the virtual ToString method inherited
35from type object. If ToString returns null, an empty string is substituted. [Example:
36using System;
194
