Tipos de Datos
Por: scar Lpez, M.Sc.
olopez@uniandino.com.co
Qu es un Tipo de Dato?
Una clase?
Una estructura?
Una interfaz?
Un delegado?
Un tipo bsico?
Un protocolo?
Puede un objeto tener ms de un tipo?
OALP-2004 All Rights Reserved
Qu es un Tipo de Dato?
Tipos de Datos en C#
Tipos Valor
Tipos Bsicos
Tipo
Rango
Tamao
-128 a 127
SByte
0 a 255
Byte
-32768 a 32767
Int16
ushort
0 a 65535
UInt16
int
-231 a 231-1
Int32
uint
0 a 232-1
UInt32
long
-263 a 263-1
Int64
sbyte
byte
short
Tipo .NET
Tipos Bsicos
Tipo
Rango
Tamao
ulong
0 a 264-1
UInt64
float
Single
double
Double
decimal
1.0E-28 a 7.9E28
precisin fija con 28-29 dgitos significativos
y la posicin del punto decimal
16
Decimal
char
1 cracter Unicode
Char
bool
true o false
Boolean
Tipo .NET
Tipo
Ejemplo
long
uint
UL
ulong
float
decimal
Cracter Representado
Comilla sencilla
Comilla doble
\\
Backslash
\0
null
\a
Alerta (beep)
\b
Retroceso
\f
Alimentacin de formulario
\n
Nueva lnea
\r
\t
Tabulacin horizontal
\v
Tabulacin vertical
\cN
Tipos Referencia
typeof - Ejemplo
1.
using System;
2.
4.
interface IMyInterface {}
struct MyStruct {}
class ClassA : IMyInterface {}
5.
namespace MyNamespace {
3.
class Test {
6.
7.
Type t1
Type t2
Type t3
Type t4
Type t5
Type t6
Type t7
Type t8
// Type
8.
9.
10.
11.
12.
13.
14.
15.
16.
= typeof(int);
= typeof(decimal);
= typeof(string);
= typeof(object);
= typeof(IMyInterface);
= typeof(MyStruct);
= typeof(ClassA);
= typeof(MyNamespace.Test);
t9 = typeof(new Test());
24.
Console.WriteLine(t1);
Console.WriteLine(t2);
Console.WriteLine(t3);
Console.WriteLine(t4);
Console.WriteLine(t5);
Console.WriteLine(t6);
Console.WriteLine(t7);
Console.WriteLine(t8);
25.
Console.ReadLine();
17.
18.
19.
20.
21.
22.
23.
26.
27.
28.
typeof
Object.GetType()
Type.GetType()
Parmetro
nombre de tipo
nombre de tipo
(como cadena)
Ejecucin
tiempo de compilacin
tiempo de ejecucin
tiempo de ejecucin
Ejemplo
typeof(MiClase)
miClase.GetType()
Type.GetType("MiClase")
Operador checked
checked {<bloque de cdigo>}
checked (<expresin>)
Operador unchecked
unchecked {<bloque de cdigo>}
unchecked (<expresin>)
using System;
2.
class MyClass {
public static void Main() {
3.
4.
5.
6.
7.
8.
9.
10.
checked {
int l = 2147483647;
int m = l + 1; // System.OverflowException
Console.WriteLine(m);
}
11.
12.
13.
14.
15.
19.
unchecked {
int n = 2147483647 + 1;
Console.WriteLine(n);
}
20.
Console.ReadLine();
16.
17.
18.
21.
22.
Boxing
Boxing - Ejemplo
1.
using System;
2.
3.
4.
int i = 99;
5.
// boxing explcito
object o = (object)i;
Console.WriteLine(o.ToString());
Console.WriteLine(o.GetType());
6.
7.
8.
11.
// boxing implcito
Console.WriteLine(i.ToString());
Console.WriteLine(i.GetType());
12.
Console.ReadLine();
9.
10.
13.
14.
Unboxing
1.
2.
3.
Unboxing - Ejemplo
1.
int i = 99;
object boxed = (object)i; // boxing
short j = (short)boxed;
// InvalidCastException
2.
3.
1.
int i = 99;
object boxed = (object)i; // boxing
double j = (double)boxed; // InvalidCastException
2.
3.
Conversiones
using System;
2.
3.
4.
5.
6.
7.
Console.WriteLine(d);
Console.WriteLine(f);
Console.WriteLine(ul);
8.
9.
10.
11.
12.
13.
14.
17.
Console.WriteLine(i);
Console.WriteLine(s);
Console.WriteLine(ui);
18.
Console.ReadLine();
15.
16.
19.
20.
}
OALP-2004 All Rights Reserved