Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created June 3, 2014 23:30
Show Gist options
  • Save Fhernd/325b94460d286a2f126e to your computer and use it in GitHub Desktop.
Save Fhernd/325b94460d286a2f126e to your computer and use it in GitHub Desktop.
Demostración del uso del operador as en C#.
using System;
namespace Recetas.Cap03
{
internal class ClaseA { }
internal class ClaseB { }
internal class Aplicacion
{
public static void Main()
{
// Arreglo de instancias Object:
object[] arregloObject = new object[6];
// Agrega elementos al arreglo `arregloObject`:
arregloObject[0] = new ClaseA();
arregloObject[1] = new ClaseB();
arregloObject[2] = "Blog xCSw";
arregloObject[3] = 13;
arregloObject[4] = 17.9;
arregloObject[5] = null;
for (int i = 0; i < arregloObject.Length; ++i)
{
// Uso del operador as para convertir a string:
string cadena = arregloObject[i] as string;
Console.Write("{0}: ", i.ToString());
if ( cadena != null)
{
Console.WriteLine("'{0}'", cadena);
}
else
{
Console.WriteLine("El elemento en {0} no es un objeto de string.", i.ToString());
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment