Advertisement
Fhernd

Usoas.cs

Jul 28th, 2014
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.23 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Recetas.Cap03
  4. {
  5.     internal class ClaseA { }
  6.     internal class ClaseB { }
  7.    
  8.     internal class Aplicacion
  9.     {
  10.         public static void Main()
  11.         {
  12.             // Arreglo de instancias Object:
  13.             object[] arregloObject = new object[6];
  14.            
  15.             // Agrega elementos al arreglo `arregloObject`:
  16.             arregloObject[0] = new ClaseA();
  17.             arregloObject[1] = new ClaseB();
  18.             arregloObject[2] = "Blog xCSw";
  19.             arregloObject[3] = 13;
  20.             arregloObject[4] = 17.9;
  21.             arregloObject[5] = null;
  22.            
  23.             for (int i = 0; i < arregloObject.Length; ++i)
  24.             {
  25.                 // Uso del operador as para convertir a string:
  26.                 string cadena = arregloObject[i] as string;
  27.                
  28.                 Console.Write("{0}: ", i.ToString());
  29.                
  30.                 if ( cadena != null)
  31.                 {
  32.                     Console.WriteLine("'{0}'", cadena);
  33.                 }
  34.                 else
  35.                 {
  36.                     Console.WriteLine("El elemento en {0} no es un objeto de string.", i.ToString());
  37.                 }
  38.             }
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement