Guest User

Untitled

a guest
May 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. public bool TryFoo(string param, out FooThing foo)
  2. {
  3. try
  4. {
  5. // do whatever
  6. return true;
  7. }
  8. catch
  9. {
  10. foo = null;
  11. return false;
  12. }
  13. }
  14.  
  15. var dic = new Dictionary<string, string>() { { "Foo", "Bar" } };
  16.  
  17. string val = String.Empty;
  18. string key = null;
  19.  
  20. dic.TryGetValue(key, out val); // oops
  21.  
  22. private int FindEntry(TKey key)
  23. {
  24. if (key == null)
  25. {
  26. ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
  27. }
  28. // more stuff
  29. }
  30.  
  31. catch( Exception ex )
  32. {
  33. Logger.Log( ex );
  34. Debug.Assert( false );
  35. foo = null;
  36. return false;
  37. }
  38.  
  39. public bool TryGetValue(T key, out U value)
  40. {
  41. IList<KeyValue> kvCollection = internalArray[key.GetHashCode() % internalArray.Length];
  42. for(KeyValue kv in kvCollection)
  43. {
  44. if(kv.Key == key)
  45. {
  46. value = kv.Value;
  47. return true;
  48. }
  49. }
  50. value = default(U);
  51. return false;
  52. }
  53.  
  54. public U GetValue(T key)
  55. {
  56. U value;
  57. if (TryGetValue(key, out value))
  58. {
  59. return value;
  60. }
  61. throw new KeyNotFoundException(key);
  62. }
Add Comment
Please, Sign In to add comment