X hits on this document

1193 views

0 shares

0 downloads

0 comments

19 / 396

C# LANGUAGE SPECIFICATION

class Test { static void Main() { int i = 123; object o = i;// Boxing int j = (int)o;// Unboxing } }

When a value of a value type is converted to type object, an object instance, also called a “box,” is allocated to hold the value, and the value is copied into that box. Conversely, when an object reference is cast to a value type, a check is made that the referenced object is a box of the correct value type, and, if the check succeeds, the value in the box is copied out.

C#’s unified type system effectively means that value types can become objects “on demand.” Because of the unification, general-purpose libraries that use type object, such as the collection classes in the .NET Framework, can be used with both reference types and value types.

There are several kinds of variables in C#, including fields, array elements, local variables, and parameters. Variables represent storage locations, and every variable has a type that determines what values can be stored in the variable, as shown by the following table.

Type of Variable

Possible Contents

Value type

A value of that exact type

object

A null reference, a reference to an object of any reference type, or a reference to a boxed value of any value type

Class type

A null reference, a reference to an instance of that class type, or a reference to an instance of a class derived from that class type

Interface type

A null reference, a reference to an instance of a class type that implements that interface type, or a reference to a boxed value of a value type that implements that interface type

Array type

A null reference, a reference to an instance of that array type, or a reference to an instance of a compatible array type

Delegate type

A null reference or a reference to an instance of that delegate type

1.4 Expressions

Expressions are constructed from operands and operators. The operators of an expression indicate which operations to apply to the operands. Examples of operators include +, -, *, /, and new. Examples of operands include literals, fields, local variables, and expressions.

When an expression contains multiple operators, the precedence of the operators controls the order in which the individual operators are evaluated. For example, the expression x + y * z is evaluated as x + (y * z) because the * operator has higher precedence than the + operator.

Most operators can be overloaded. Operator overloading permits user-defined operator implementations to be specified for operations where one or both of the operands are of a user-defined class or struct type.

The following table summarizes C#’s operators, listing the operator categories in order of precedence from highest to lowest. Operators in the same category have equal precedence.

6Copyright Microsoft Corporation 1999-2003. All Rights Reserved.

Document info
Document views1193
Page views1193
Page last viewedWed Jan 18 20:18:07 UTC 2017
Pages396
Paragraphs9401
Words133190

Comments