- •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
1When x is of type int or long, the low-order bits of x are discarded, the remaining bits are shifted
2right, and the high-order empty bit positions are set to zero if x is non-negative and set to one if x is
3negative.
4When x is of type uint or ulong, the low-order bits of x are discarded, the remaining bits are shifted
5right, and the high-order empty bit positions are set to zero.
6For the predefined operators, the number of bits to shift is computed as follows:
7• When the type of x is int or uint, the shift count is given by the low-order five bits of count. In other
8 words, the shift count is computed from count & 0x1F.
9• When the type of x is long or ulong, the shift count is given by the low-order six bits of count. In
10other words, the shift count is computed from count & 0x3F.
11If the resulting shift count is zero, the shift operators simply return the value of x.
12Shift operations never cause overflows and produce the same results in checked and unchecked contexts.
13When the left operand of the >> operator is of a signed integral type, the operator performs an arithmetic
14shift right wherein the value of the most significant bit (the sign bit) of the operand is propagated to the
15high-order empty bit positions. When the left operand of the >> operator is of an unsigned integral type, the
16operator performs a logical shift right wherein high-order empty bit positions are always set to zero. To
17perform the opposite operation of that inferred from the operand type, explicit casts can be used. [Example:
18If x is a variable of type int, the operation unchecked((int)((uint)x >> y)) performs a logical shift
19right of x. end example]
2014.9 Relational and type-testing operators
21The ==, !=, <, >, <=, >=, is and as operators are called the relational and type-testing operators.
22relational-expression:
23 |
shift-expression |
24 |
relational-expression < shift-expression |
25 |
relational-expression > shift-expression |
26 |
relational-expression <= shift-expression |
27 |
relational-expression >= shift-expression |
28 |
relational-expression is type |
29 |
relational-expression as type |
30 |
equality-expression: |
31 |
relational-expression |
32 |
equality-expression == relational-expression |
33 |
equality-expression != relational-expression |
34The is operator is described in §14.9.9 and the as operator is described in §14.9.10.
35The ==, !=, <, >, <= and >= operators are comparison operators. For an operation of the form x op y, where
36op is a comparison operator, overload resolution (§14.2.4) is applied to select a specific operator
37implementation. The operands are converted to the parameter types of the selected operator, and the type of
38the result is the return type of the operator. If both operands of an equality-expression have the null type
39(and hence the null value as well), then overload resolution is not performed and the expression evaluates
40to a contant value of true or false according to whether the operator is == or !=.
41The predefined comparison operators are described in the following subclauses. All predefined comparison
42operators return a result of type bool, as described in the following table.
43
198
Chapter 14 Expressions
Operation |
Result |
|
|
x == y |
true if x is equal to y, false otherwise |
|
|
x != y |
true if x is not equal to y, false otherwise |
|
|
x < y |
true if x is less than y, false otherwise |
|
|
x > y |
true if x is greater than y, false otherwise |
|
|
x <= y |
true if x is less than or equal to y, false otherwise |
|
|
x >= y |
true if x is greater than or equal to y, false otherwise |
|
|
1
214.9.1 Integer comparison operators
3The predefined integer comparison operators are:
4bool operator ==(int x, int y);
5bool operator ==(uint x, uint y);
6bool operator ==(long x, long y);
7bool operator ==(ulong x, ulong y);
8void operator ==(long x, ulong y);
9void operator ==(ulong x, long y);
10bool operator !=(int x, int y);
11bool operator !=(uint x, uint y);
12bool operator !=(long x, long y);
13bool operator !=(ulong x, ulong y);
14void operator !=(long x, ulong y);
15void operator !=(ulong x, long y);
16bool operator <(int x, int y);
17bool operator <(uint x, uint y);
18bool operator <(long x, long y);
19bool operator <(ulong x, ulong y);
20void operator <(long x, ulong y);
21void operator >(ulong x, long y);
22bool operator >(int x, int y);
23bool operator >(uint x, uint y);
24bool operator >(long x, long y);
25bool operator >(ulong x, ulong y);
26void operator >(long x, ulong y);
27void operator >(ulong x, long y);
28bool operator <=(int x, int y);
29bool operator <=(uint x, uint y);
30bool operator <=(long x, long y);
31bool operator <=(ulong x, ulong y);
32void operator <=(long x, ulong y);
33void operator <=(ulong x, long y);
34bool operator >=(int x, int y);
35bool operator >=(uint x, uint y);
36bool operator >=(long x, long y);
37bool operator >=(ulong x, ulong y);
38void operator >=(long x, ulong y);
39void operator >=(ulong x, long y);
40Each of these operators compares the numeric values of the two integer operands and returns a bool
41value that indicates whether the particular relation is true or false. The operators with void return
42type always produce a compile-time error. Consequently, it is an error for one operand to be of type
43long and the other to be of type ulong.
4414.9.2 Floating-point comparison operators
45The predefined floating-point comparison operators are:
199
C# LANGUAGE SPECIFICATION
1bool operator ==(float x, float y);
2bool operator ==(double x, double y);
3bool operator !=(float x, float y);
4bool operator !=(double x, double y);
5bool operator <(float x, float y);
6bool operator <(double x, double y);
7bool operator >(float x, float y);
8bool operator >(double x, double y);
9bool operator <=(float x, float y);
10bool operator <=(double x, double y);
11bool operator >=(float x, float y);
12bool operator >=(double x, double y);
13The operators compare the operands according to the rules of the IEC 60559 standard:
14• If either operand is NaN, the result is false for all operators except !=, for which the result is true.
15For any two operands, x != y always produces the same result as !(x == y). However, when one or
16both operands are NaN, the <, >, <=, and >= operators do not produce the same results as the logical
17negation of the opposite operator. [Example: If either of x and y is NaN, then x < y is false, but
18!(x >= y) is true. end example]
19• When neither operand is NaN, the operators compare the values of the two floating-point operands with
20respect to the ordering
21–∞ < –max < … < –min < –0.0 == +0.0 < +min < … < +max < +∞
22where min and max are the smallest and largest positive finite values that can be represented in the given
23floating-point format. Notable effects of this ordering are:
24o Negative and positive zeros are considered equal.
25o A negative infinity is considered less than all other values, but equal to another negative infinity.
26o A positive infinity is considered greater than all other values, but equal to another positive infinity.
2714.9.3 Decimal comparison operators
28The predefined decimal comparison operators are:
29bool operator ==(decimal x, decimal y);
30bool operator !=(decimal x, decimal y);
31bool operator <(decimal x, decimal y);
32bool operator >(decimal x, decimal y);
33bool operator <=(decimal x, decimal y);
34bool operator >=(decimal x, decimal y);
35Each of these operators compares the numeric values of the two decimal operands and returns a bool value
36that indicates whether the particular relation is true or false. Each decimal comparison is equivalent to
37using the corresponding relational or equality operator of type System.Decimal.
3814.9.4 Boolean equality operators
39The predefined boolean equality operators are:
40bool operator ==(bool x, bool y);
41bool operator !=(bool x, bool y);
42The result of == is true if both x and y are true or if both x and y are false. Otherwise, the result is
43false.
44The result of != is false if both x and y are true or if both x and y are false. Otherwise, the result is
45true. When the operands are of type bool, the != operator produces the same result as the ^ operator.
200
Chapter 14 Expressions
114.9.5 Enumeration comparison operators
2Every enumeration type implicitly provides the following predefined comparison operators:
3bool operator ==(E x, E y);
4bool operator !=(E x, E y);
5bool operator <(E x, E y);
6bool operator >(E x, E y);
7bool operator <=(E x, E y);
8bool operator >=(E x, E y);
9The result of evaluating x op y, where x and y are expressions of an enumeration type E with an underlying
10type U, and op is one of the comparison operators, is exactly the same as evaluating ((U)x) op ((U)y). In
11other words, the enumeration type comparison operators simply compare the underlying integral values of
12the two operands. These operators are only considered by overload resolution (§14.2.4) when one of the
13actual operands is of type E.
1414.9.6 Reference type equality operators
15Every class type C implicitly provides the following predefined reference type equality operators:
16bool operator ==(C x, C y);
17bool operator !=(C x, C y);
18unless predefined equality operators otherwise exists for C (for example, when C is string or
19System.Delegate).
20The operators return the result of comparing the two references for equality or non-equality.
21There are special rules for determining when a reference type equality operator is applicable. For an
22equality-expression with operands of type A and B, define A0 as follows:
23• If A is a type parameter that does not have the value type constraint (§26.7), let A0 be the effective base
24class of A.
25• Otherwise, if A is an interface type, a delegate type, an array type, a class type, or the null type, let A0
26be the same as A.
27• Otherwise, no reference type equality operator is applicable.
28Now define A1 as follows:
29• If A0 is an interface type, a delegate type, System.Delegate, or string, let A1 be object.
30• Otherwise, if A0 is an array type, let A1 be System.Array.
31• Otherwise, A0 is the null type or a class type and let A1 be the same as A0.
32Define B0 and B1 similarly. Now determine if any reference type equality operators are applicable as follows:
33• If both of the types A and B are the null type, then overload resolution is not performed and the result is
34a constant true or false, as specified in §14.9.
35• Otherwise, if A is the null type, then the reference type equality operator for B1 is applicable. If B is a
36type parameter and at run-time the type parameter is a value type, the result of the comparison is false.
37• Otherwise, if B is the null type, then the reference type equality operator for A1 is applicable. If A is a
38type parameter and at run-time the type parameter is a value type, the result of the comparison is false.
39• Otherwise, if either A or B is a type parameter that is not known to be a reference type (§26.7), then no
40reference type equality operator is applicable.
41• Otherwise, if there is no identity or reference conversion (implicit or explicit) from A0 to B0 or no
42identity or reference conversion (implicit or explicit) from B0 to A0, then no reference type equality
43operator is applicable.
201
C# LANGUAGE SPECIFICATION
1• Otherwise, if there is an identity or implicit reference conversion from A1 to B1, then the reference type
2equality operator for B1 is applicable.
3• Otherwise, if there is an implicit reference conversion from B1 to A1, then the reference type equality
4operator for A1 is applicable.
5• Otherwise no reference type equality operator is applicable.
6[Note: Notable implications of these rules are:
7• The predefined reference type equality operators cannot be used to compare two references that are
8known to be different at compile-time. [Example: If the compile-time types of the operands are two class
9types A and B, and if neither A nor B derives from the other, then it would be impossible for the two
10operands to reference the same object and no reference type equality operator is applicable. Similarly, if
11A is a sealed class and B is an interface that A does not implement, then no reference type equality
12operator is applicable. end example]
13• The predefined reference type equality operators do not permit value type operands to be compared.
14• The predefined reference type equality operators never cause boxing operations to occur for their
15operands. It would be meaningless to perform such boxing operations, since references to the newly
16allocated boxed instances would necessarily differ from all other references.
17end note]
18When the operator overload resolution (§14.2.4) rules would pick an equality operator other than a reference
19type equality operator, selection of a reference type equality operator can be forced by explicitly casting one
20or both operands to type object. [Example: The example
21using System;
22class Test
23{
24 |
static void Main() { |
25 |
string s = "Test"; |
26 |
string t = string.Copy(s); |
27 |
Console.WriteLine(s == t); |
28 |
Console.WriteLine((object)s == t); |
29 |
Console.WriteLine(s == (object)t); |
30 |
Console.WriteLine((object)s == (object)t); |
31}
32}
33produces the output
34True
35False
36False
37False
38The s and t variables refer to two distinct string instances containing the same characters. The first
39comparison outputs True because the predefined string equality operator (§14.9.7) is selected when both
40operands are of type string. The remaining comparisons all output False because a predefined reference
41type equality operator is selected when one or both of the operands are of type object.
42Note that the above technique is not meaningful for value types. The example
43class Test
44{
45 |
static void Main() { |
46 |
int i = 123; |
47 |
int j = 123; |
48 |
System.Console.WriteLine((object)i == (object)j); |
49}
50}
51outputs False because the casts create references to two separate instances of boxed int values. end
52example]
202
