Tipos de Datos Programación C#
Tipos de Datos Programación C#
Tipos de Datos
¿Una clase?
¿Una estructura?
¿Una interfaz?
¿Un delegado?
¿Un tipo básico?
¿Un protocolo?
¿Puede un objeto tener más de un tipo?
2. interface IMyInterface {}
3. struct MyStruct {}
4. class ClassA : IMyInterface {}
5. namespace MyNamespace {
6. class Test {
8. Type t1 = typeof(int);
9. Type t2 = typeof(decimal);
10. Type t3 = typeof(string);
11. Type t4 = typeof(object);
12. Type t5 = typeof(IMyInterface);
13. Type t6 = typeof(MyStruct);
14. Type t7 = typeof(ClassA);
15. Type t8 = typeof(MyNamespace.Test);
16. // Type t9 = typeof(new Test());
17. Console.WriteLine(t1);
18. Console.WriteLine(t2);
19. Console.WriteLine(t3);
20. Console.WriteLine(t4);
21. Console.WriteLine(t5);
22. Console.WriteLine(t6);
23. Console.WriteLine(t7);
24. Console.WriteLine(t8);
25. Console.ReadLine();
26. }
27. }
28. }
2. class MyClass {
11. checked {
12. int l = 2147483647;
13. int m = l + 1; // System.OverflowException
14. Console.WriteLine(m);
15. }
16. unchecked {
17. int n = 2147483647 + 1;
18. Console.WriteLine(n);
19. }
20. Console.ReadLine();
21. }
22. }
4. int i = 99;
5. // boxing explícito
6. object o = (object)i;
7. Console.WriteLine(o.ToString());
8. Console.WriteLine(o.GetType());
9. // boxing implícito
10. Console.WriteLine(i.ToString());
11. Console.WriteLine(i.GetType());
12. Console.ReadLine();
13. }
14. }
1. int i = 99;
2. object boxed = (object)i; // boxing
3. int j = (int)boxed; // unboxing
1. int i = 99;
2. object boxed = (object)i; // boxing
3. short j = (short)boxed; // InvalidCastException
1. int i = 99;
2. object boxed = (object)i; // boxing
3. double j = (double)boxed; // InvalidCastException
8. Console.WriteLine(d);
9. Console.WriteLine(f);
10. Console.WriteLine(ul);
15. Console.WriteLine(i);
16. Console.WriteLine(s);
17. Console.WriteLine(ui);
18. Console.ReadLine();
19. }
20. }
OALP-2004 All Rights Reserved
Conversiones Explícitas de Tipos Referencia