Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- double? result
- double denominator = 0;
- double Numerator = 100;
- result = Numerator / NullIf(denominator, 0);
- result = Numerator / denominator.NullIf(0);
- result = Numerator / Object.NullIf(denominator, 0);
- result = Numerator / (denominator == 0 ? (double?)null : denomiantor);
- Nullable<T> NullIf<T>(T left, T right)
- {
- return left == right ? (T?)null : left;
- }
- result = Numerator / NullIf(denominator, 0);
- public static Nullable<T> NullIf<T>(T first, T second) where T : struct
- {
- if(first == second)
- return new Nullable<T>();
- return new Nullable<T>(first);
- }
- double? result
- double denominator = 0;
- double Numerator = 100;
- result = denominator == 0 ? (double?)null : Numerator / denominator;
- result = x ?? 0;
- result = x.HasValue? x.Value : 0;
- public static T? NullIf<T>(T left, T right) where T : struct
- {
- return EqualityComparer<T>.Default.Equals(left, right) ? (T?)null : left;
- }
- public static T? NullIf<T>(this T value, Func<T,bool> isConsideredNull)
- {
- if(value == null)
- {
- return null;
- }
- return isConsideredNull(value) ? (T?)null : value;
- }
- string test = "NULL";
- test.NullIf((x)=> x.Equals("NULL"));
- test.NullIf((x)=> x == "NULL");
- test.NullIf((x)=> x.Equals("NULL",StringComparison.InvariantCultureIgnoreCase));
- public static bool AnyNull<T>(this IEnumerable<T> values) where T : class
- {
- if (values == null)
- {
- throw new ArgumentNullException(nameof(values));
- }
- return values.Any(obj => obj == null);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement