LENGUAJE CSHARP
PALABRAS RESERVADAS
Keywords are predefined reserved identifiers that have special meanings to the compiler. They cannot be used as identifiers in your program unless they include @ as a prefix. For example @if is a legal identifier but if is not because it is a keyword.
abstract
event
new
struct
as
explicit
null
switch
base
extern
object
this
bool
false
operator
throw
break
finally
out
true
byte
fixed
override
try
case
float
params
typeof
catch
for
private
uint
char
foreach
protected
ulong
checked
goto
public
unchecked
class
if
readonly
unsafe
const
implicit
ref
ushort
continue
in
return
using
decimal
int
sbyte
virtual
default
interface
sealed
volatile
delegate
internal
short
void
do
is
sizeof
while
double
lock
stackalloc
else
long
static
enum
namespace
string
FUENTE: © 2001 Microsoft Corporation. All rights reserved.
C# Operators
C# provides a large set of operators which are symbols that specify which operations to perform in an expression. C# predefines the usual arithmetic and logical operators as well as a variety of others as shown in the following table. In addition many operators can be overloaded by the user thus changing their meaning when applied to a user-defined type.
Operator category
Operators
Arithmetic
+ - * / %
Logical (boolean and bitwise)
& | ^ ! ~ && || true false
String concatenation
+
Increment decrement
++ –
Shift
« »
Relational
== != < > ⇐ >=
Assignment
= += -= *= /= %= &= |= ^= «= »=
Member access
.
Indexing
[]
Cast
()
Conditional
?:
Delegate concatenation and removal
+ -
Object creation
new
Type information
is sizeof typeof
Overflow exception control
checked unchecked
Indirection and Address
* → [] &
Arithmetic Overflow
The arithmetic operators (+ - * /) can produce results that are outside the range of possible values for the numeric type involved. You should refer to the C# Language Reference section on a particular operator for details but in general:
* Integer arithmetic overflow either throws an OverflowException or
discards the most significant bits of the result (see below). Integer division by zero always throws a DivideByZeroException.
* Floating-point arithmetic overflow or division by zero never throws
an exception because floating-point types are based on IEEE 754 and so have provisions for representing infinity and NaN (Not a Number).
* Decimal arithmetic overflow always throws an OverflowException.
Decimal division by zero always throws a DivideByZeroException.
When integer overflow occurs what happens depends on the execution context which can be checked or unchecked. In a checked context an OverflowException is thrown. In an unchecked context the most significant bits of the result are discarded and execution continues. Thus C# gives you the choice of handling or ignoring overflow.
In addition to the arithmetic operators integral-type to integral-type casts can cause overflow (for example casting a long to an int) and are subject to checked or unchecked execution. Also note that bitwise operators and shift operators never cause overflow.
FUENTE: © 2001 Microsoft Corporation. All rights reserved.
Types Reference Tables
The following reference tables summarize the C# types:
* Built-in types * Integral types * Floating-point types * Default values * Value types * Implicit numeric conversions * Explicit numeric conversions
For information on formatting the output of numeric types see Formatting Numeric Results Table.
FUENTE: © 2001 Microsoft Corporation. All rights reserved.
Built-in Types Table
The following table shows the keywords for built-in C# types which are aliases of predefined types in the System namespace.
C# Type
.NET Framework type
bool
System.Boolean
byte
System.Byte
sbyte
System.SByte
char
System.Char
decimal
System.Decimal
double
System.Double
float
System.Single
int
System.Int32
uint
System.UInt32
long
System.Int64
ulong
System.UInt64
object
System.Object
short
System.Int16
ushort
System.UInt16
string
System.String
Remarks
All of the types in the table except object and string are referred to as simple types.
The C# type keywords and their aliases are interchangeable. For example you can declare an integer variable by using either of the following declarations:
int x = 123; System.Int32 x = 123;
To display the actual type for any C# type use the system method GetType(). For example the following statement displays the system alias that represents the type of myVariable:
Console.WriteLine(myVariable.GetType());
You can also use the typeof operator
FUENTE: © 2001 Microsoft Corporation. All rights reserved.
Integral Types Table
The following table shows the sizes and ranges of the integral types which constitute a subset of simple types.
Type
Range
Size
sbyte
-128 to 127
Signed 8-bit integer
byte
0 to 255
Unsigned 8-bit integer
char
U+0000 to U+ffff
Unicode 16-bit character
short
-32768 to 32767
Signed 16-bit integer
ushort
0 to 65535
Unsigned 16-bit integer
int
-2147483648 to 2147483647
Signed 32-bit integer
uint
0 to 4294967295
Unsigned 32-bit integer
long
-9223372036854775808 to 9223372036854775807
Signed 64-bit integer
ulong
0 to 18446744073709551615
Unsigned 64-bit integer
Remarks
If the value represented by an integer literal exceeds the range of ulong a compilation error will occur.
FUENTE: © 2001 Microsoft Corporation. All rights reserved.
Floating-Point Types Table
The following table shows the precision and approximate ranges for the floating-point types.
Type
Approximate range
Precision
float
±1.5 × 10-45 to ±3.4 × 1038
7 digits
double
±5.0 × 10-324 to ±1.7 × 10308
15-16 digits
FUENTE: © 2001 Microsoft Corporation. All rights reserved.
Default Values Table
The following table shows the default values of value types returned by the default constructors. Default constructors are invoked by using the new operator for example:
int myInt = new int();
The preceding statement has the same effect as the following statement:
int myInt = 0;
Remember that using uninitialized variables in C# is not allowed.
Value type
Default value
bool
false
byte
0
char
'\0'
decimal
0.0M
double
0.0D
enum
The value produced by the expression (E)0 where E is the enum identifier.
float
0.0F
int
0
long
0L
sbyte
0
short
0
struct
The value produced by setting all value-type fields to their default values and all reference-type fields to null.
uint
0
ulong
0
ushort
0
FUENTE: © 2001 Microsoft Corporation. All rights reserved.
Value Types Table
The following table lists the C# value types by category.
Value type
Category
bool
Boolean
byte
Unsigned numeric integral
char
Unsigned numeric integral
decimal
Numeric decimal
double
Numeric floating-point
enum
Enumeration
float
Numeric floating-point
int
Signed numeric integral
long
Signed numeric integral
sbyte
Signed numeric integral
short
Signed numeric integral
struct
User-defined structure
uint
Unsigned numeric integral
ulong
Unsigned numeric integral
ushort
Unsigned numeric integral
FUENTE: © 2001 Microsoft Corporation. All rights reserved.
Implicit Numeric Conversions Table
The following table shows the predefined implicit numeric conversions. Implicit conversions might occur in many situations including method invoking and assignment statements.
From
To
sbyte
short int long float double or decimal
byte
short ushort int uint long ulong float double or decimal
short
int long float double or decimal
ushort
int uint long ulong float double or decimal
int
long float double or decimal
uint
long ulong float double or decimal
long
float double or decimal
char
ushort int uint long ulong float double or decimal
float
double
ulong
float double or decimal
Remarks
* The conversions from int uint or long to float and from long to
double may cause a loss of precision but not a loss of magnitude.
* There are no implicit conversions to the char type. * There are no implicit conversions between floating-point types and
the decimal type.
* A constant expression of type int can 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.
FUENTE: © 2001 Microsoft Corporation. All rights reserved.
Explicit Numeric Conversions Table
Explicit numeric conversion is used to convert any numeric type to any other numeric type for which there is no implicit conversion by using a cast expression. The following table shows these conversions.
From
To
sbyte
byte ushort uint ulong or char
byte
sbyte or char
short
sbyte byte ushort uint ulong or char
ushort
sbyte byte short or char
int
sbyte byte short ushort uint ulong or char
uint
sbyte byte short ushort int or char
long
sbyte byte short ushort int uint ulong or char
ulong
sbyte byte short ushort int uint long or char
char
sbyte byte or short
float
sbyte byte short ushort int uint long ulong char or decimal
double
sbyte byte short ushort int uint long ulong char float or decimal
decimal
sbyte byte short ushort int uint long ulong char float or double
Remarks
* The explicit numeric conversion may cause loss of precision or result
in throwing exceptions.
* When you convert a float double or decimal value to an integral
type this value is rounded towards zero to the nearest integral value. If the resulting integral value is outside the range of the destination type an InvalidCastException is thrown.
* When you convert double to float the double value is rounded to the
nearest float value. If the double value is too small or too large to fit into the destination type the result will be zero or infinity.
* When you convert float or double to decimal the source value is
converted to decimal representation and rounded to the nearest number after the 28th decimal place if required. Depending on the value of the source value one of the following results may occur:
*
o If the source value is too small to be represented as a decimal the result becomes zero. o If the source value is NaN (not a number) infinity or too large to be represented as a decimal an InvalidCastException is thrown.
* When you convert decimal to float or double the decimal value is
rounded to the nearest double or float value.
* FUENTE: © 2001 Microsoft Corporation. All rights reserved.
Formatting Numeric Results Table
You can format numeric results by using the String.Format method or through the Console.Write method which calls String.Format. The format is specified using format strings. The following table contains the supported standard format strings. The format string takes the form Axx where A is the format specifier and xx is the precision specifier. The format specifier controls the type of formatting applied to the numerical value and the precision specifier controls the number of significant digits or decimal places of the formatted output.
For more information on standard and custom formatting strings see Formatting Overview. For more information on the String.Format method see String.Format Method.
Character
Description
Examples
Output
C or c
Currency
Console.Write(”{0:C}” 2.5);
Console.Write(”{0:C}” -2.5);
$2.50
($2.50)
D or d
Decimal
Console.Write(”{0:D5}” 25);
00025
E or e
Scientific
Console.Write(”{0:E}” 250000);
2.500000E+005
F or f
Fixed-point
Console.Write(”{0:F2}” 25);
Console.Write(”{0:F0}” 25);
25.00
25
G or g
General
Console.Write(”{0:G}” 2.5);
2.5
N or n
Number
Console.Write(”{0:N}” 2500000);
2500000.00
X or x
Hexadecimal
Console.Write(”{0:X}” 250);
Console.Write(”{0:X}” 0xffff);
FA
FFFF
FUENTE: © 2001 Microsoft Corporation. All rights reserved.