News for hackers. Stuff that matters.
Re:Nullable vs. Nullable in C# by manuc66 on Friday August 29, 2025 @05:07AM (#45061846)
@Bogdanp maybe this is another way to express it:

public static class EnumerableExtensions {

public static IEnumerable<TR> SelectNotNull<T, TR>( this IEnumerable<T> source, Func<T, TR?> fn) where TR : class { return source.Select(fn) .Where(it => it != null) .OfType<T>(); }

public static IEnumerable<TR> SelectNotNull<T, TR>( this IEnumerable<T> source, Func<T, TR?> fn) where TR : struct { return source.Select(fn) .Where(it => it != null) .Select(item => item.Value); } }

Re:Nullable vs. Nullable in C# by Uvix on Friday August 29, 2025 @08:10AM (#45063009)
Both look like they’ll work. For the struct case I think yours is better. For the class case I think using Cast() like the original instead of OfType() makes more sense - you don’t need to filter out other types from the enumerable, so cast once instead of casting then performing a useless check on the result.
Re:Nullable vs. Nullable in C# by nick_ on Friday August 29, 2025 @03:13PM (#45068190)
I'm pretty sure you only need the OfType call. It filters out nulls already.