Table of Contents

LENGUAJE CSHARP

PALABRAS RESERVADAS C# PDA


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:

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:

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 -32,768 to 32,767 Signed 16-bit integer
ushort 0 to 65,535 Unsigned 16-bit integer
int -2,147,483,648 to 2,147,483,647 Signed 32-bit integer
uint 0 to 4,294,967,295 Unsigned 32-bit integer
long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Signed 64-bit integer
ulong 0 to 18,446,744,073,709,551,615 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
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
long 0L
sbyte
short
struct The value produced by setting all value-type fields to their default values and all reference-type fields to null.
uint
ulong
ushort

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

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

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); 2,500,000.00
X or x Hexadecimal Console.Write(”{0:X}”, 250); Console.Write(”{0:X}”, 0xffff); FA FFFF

FUENTE: © 2001 Microsoft Corporation. All rights reserved.

Politica de Privacidad