ModelExtensions.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 
  2. using System.Collections.Generic;
  3. namespace MediaBrowser.Installer.Code
  4. {
  5. /// <summary>
  6. /// Class ModelExtensions
  7. /// </summary>
  8. static class ModelExtensions
  9. {
  10. /// <summary>
  11. /// Values the or default.
  12. /// </summary>
  13. /// <param name="str">The STR.</param>
  14. /// <param name="def">The def.</param>
  15. /// <returns>System.String.</returns>
  16. public static string ValueOrDefault(this string str, string def = "")
  17. {
  18. return string.IsNullOrEmpty(str) ? def : str;
  19. }
  20. /// <summary>
  21. /// Helper method for Dictionaries since they throw on not-found keys
  22. /// </summary>
  23. /// <typeparam name="T"></typeparam>
  24. /// <typeparam name="U"></typeparam>
  25. /// <param name="dictionary">The dictionary.</param>
  26. /// <param name="key">The key.</param>
  27. /// <param name="defaultValue">The default value.</param>
  28. /// <returns>``1.</returns>
  29. public static U GetValueOrDefault<T, U>(this Dictionary<T, U> dictionary, T key, U defaultValue)
  30. {
  31. U val;
  32. if (!dictionary.TryGetValue(key, out val))
  33. {
  34. val = defaultValue;
  35. }
  36. return val;
  37. }
  38. }
  39. }