Created
July 7, 2014 21:37
-
-
Save Fhernd/61f23298fe343aa3f4bf to your computer and use it in GitHub Desktop.
Uso del método TryGetValue de Dictionary (System.Collections.Generic).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace Articulos.Cap04.Excepciones.Parte2 | |
{ | |
public sealed class UsoTryGetValue | |
{ | |
public static void Main() | |
{ | |
// Creación del diccionario: | |
Dictionary<string, string> abrirCon = new Dictionary<string, string>(); | |
// Agregación de extensiones: | |
abrirCon.Add (".html", "Internet Explorer"); | |
abrirCon.Add (".cs", "Notepad++ Portable (PortableApps Launcher)"); | |
abrirCon.Add (".mp3", "Winamp"); | |
abrirCon.Add (".camrec", "Camtasia Studio"); | |
abrirCon.Add (".jpg", "Pain.NET"); | |
// Uso del patrón TryXXX: | |
Console.WriteLine ("\nUso del patrón TryXXX:"); | |
string aplicacion = String.Empty; | |
if (abrirCon.TryGetValue (".tif", out aplicacion)) | |
{ | |
Console.WriteLine ("\nPara la extensión `.tif` la aplicación usada es {0}.", aplicacion); | |
} | |
else | |
{ | |
Console.WriteLine ("\nLa extensión `.tif` no tiene una aplicación asociada.\n"); | |
} | |
// Uso del modo de acceso basado en indizador ('indexer'). | |
// Este modo genera una excepción si la aplicación a la | |
// extensión pasada como índice no existe: | |
Console.WriteLine ("\nUso del modo de acceso basado en indizador:"); | |
try | |
{ | |
Console.WriteLine ("\nPara la extensión `.tif` la aplicación usada es {0}.", abrirCon[".tif"]); | |
} | |
catch (KeyNotFoundException) | |
{ | |
Console.WriteLine ("\nLa extensión `.tif` no tiene una aplicación asociada.\n"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment