Scrigroup - Documente si articole

Username / Parola inexistente      

Home Documente Upload Resurse Alte limbi doc  

AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows

Constant expressions

C sharp

+ Font mai mare | - Font mai mic


Trimite pe Messenger
Standard conversions
End points and reachability
Definite assignment
Constant expressions
Static constructors
Assignment operators
The empty statement


An expression is either a conditional-expression or an assignment.


Constant expressions

A constant-expression is an expression that can be fully evaluated at compile-time.


The type of a constant expression can be one of the following: sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, string, any enumeration type, or the null type. The following constructs are permitted in constant expressions:

Literals (including the null literal).

References to const members of class and struct types.

References to members of enumeration types.

Parenthesized sub-expressions.

Cast expressions, provided the target type is one of the types listed above.

The predefined +, , !, and ~ unary operators.

The predefined +, , *, /, %, <<, >>, &, |, ^, &&, ||, ==, !=, <, >, <=, and => binary operators, provided each operand is of a type listed above.

The ?: conditional operator.

Whenever an expression is of one of the types listed above and contains only the constructs listed above, the expression is evaluated at compile-time. This is true even if the expression is a sub-expression of a larger expression that contains non-constant constructs.

The compile-time evaluation of constant expressions uses the same rules as run-time evaluation of non-constant expressions, except that where run-time evaluation would have thrown an exception, compile-time evaluation causes a compile-time error to occur.

Unless a constant expression is explicitly placed in an unchecked context, overflows that occur in integral-type arithmetic operations and conversions during the compile-time evaluation of the expression always cause compile-time errors (7.5.12).

Constant expressions occur in the contexts listed below. In these contexts, an error occurs if an expression cannot be fully evaluated at compile-time.

Constant declarations (10.3).

Enumeration member declarations (14.3).

case labels of a switch statement (8.7.2).

goto case statements (8.9.3).

Dimension lengths in an array creation expression ( that includes an initializer.

Attributes (17).

An implicit constant expression conversion (6.1.6) permits a constant expression of type int to be converted to sbyte, byte, short, ushort, uint, or ulong, provided the value of the constant expression is within the range of the destination type.

Boolean expressions

A boolean-expression is an expression that yields a result of type bool.


The controlling conditional expression of an if-statement (8.7.1), while-statement (8.8.1), do-statement (8.8.2), or for-statement (8.8.3) is a boolean-expression. The controlling conditional expression of the ?: operator (7.12) follows the same rules as a boolean-expression, but for reasons of operator precedence is classified as a conditional-or-expression.

A boolean-expression is required to be of a type that can be implicitly converted to bool or of a type that implements operator true. If neither requirement is satisfied, a compile-time error occurs.

When a boolean expression is of a type that cannot be implicitly converted to bool but does implement operator true, then following evaluation of the expression, the operator true implementation provided by that type is invoked to produce a bool value.

The DBBool struct type in 11.4.2 provides an example of a type that implements operator true and operator false.

Politica de confidentialitate



Vizualizari: 701
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2022 . All rights reserved

Distribuie URL

Adauga cod HTML in site