Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var dictionary = new Dictionary<string, string>();
- var val = dictionary["mykey"];
- string val;
- if(dictionary.TryGetValue("mykey", out val))
- {
- //The key was found. The value is in val.
- }
- else
- {
- //The key was not present.
- }
- private void Application_UnhandledException(object sender,
- ApplicationUnhandledExceptionEventArgs e)
- {
- var stackTrace = e.ExceptionObject.StackTrace;
- //Log the stackTrace somewhere.
- }
- private void Application_UnhandledException(object sender,
- ApplicationUnhandledExceptionEventArgs e)
- {
- if (e.ExceptionObject is KeyNotFoundException)
- {
- //This was a KeyNotFoundException
- }
- }
- public static TValue GetOrThrow<TKey,TValue>(this IDictionary<TKey,TValue> d, TKey key)
- {
- try
- {
- return d[key];
- }
- catch(KeyNotFoundException ex)
- {
- throw new KeyNotFoundException(key.ToString()
- + " was not found in the dictionary");
- }
- }
- var serializedKey = Newtonsoft.Json.JsonConvert.SerializeObject(
- key,
- new JsonSerializerSettings
- {
- //make it easy for humans to read
- Formatting = Formatting.Indented,
- //don't break on loops...that would cause a new error that hides the KeyNotFound!
- ReferenceLoopHandling = ReferenceLoopHandling.Ignore
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement