Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. double? result
  2. double denominator = 0;
  3. double Numerator = 100;
  4. result = Numerator / NullIf(denominator, 0);
  5. result = Numerator / denominator.NullIf(0);
  6. result = Numerator / Object.NullIf(denominator, 0);
  7.  
  8. result = Numerator / (denominator == 0 ? (double?)null : denomiantor);
  9.  
  10. Nullable<T> NullIf<T>(T left, T right)
  11. {
  12. return left == right ? (T?)null : left;
  13. }
  14.  
  15. result = Numerator / NullIf(denominator, 0);
  16.  
  17. public static Nullable<T> NullIf<T>(T first, T second) where T : struct
  18. {
  19. if(first == second)
  20. return new Nullable<T>();
  21. return new Nullable<T>(first);
  22. }
  23.  
  24. double? result
  25. double denominator = 0;
  26. double Numerator = 100;
  27. result = denominator == 0 ? (double?)null : Numerator / denominator;
  28.  
  29. result = x ?? 0;
  30.  
  31. result = x.HasValue? x.Value : 0;
  32.  
  33. public static T? NullIf<T>(T left, T right) where T : struct
  34. {
  35. return EqualityComparer<T>.Default.Equals(left, right) ? (T?)null : left;
  36. }
  37.  
  38. public static T? NullIf<T>(this T value, Func<T,bool> isConsideredNull)
  39. {
  40. if(value == null)
  41. {
  42. return null;
  43. }
  44. return isConsideredNull(value) ? (T?)null : value;
  45. }
  46.  
  47. string test = "NULL";
  48.  
  49. test.NullIf((x)=> x.Equals("NULL"));
  50. test.NullIf((x)=> x == "NULL");
  51. test.NullIf((x)=> x.Equals("NULL",StringComparison.InvariantCultureIgnoreCase));
  52.  
  53. public static bool AnyNull<T>(this IEnumerable<T> values) where T : class
  54. {
  55. if (values == null)
  56. {
  57. throw new ArgumentNullException(nameof(values));
  58. }
  59. return values.Any(obj => obj == null);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement