I have this code and I don’t understand why the last line gives me a warning if the previous two don’t:
<code> [return: NotNull]
public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T>? sourceEnumerable) => sourceEnumerable ?? Array.Empty<T>();
public static void ConfigurationSectionTest(IConfigurationSection? cfg)
{
IEnumerable<IConfigurationSection>? myChildren = cfg?.GetChildren();
IEnumerable<IConfigurationSection> safeChildren = myChildren.EmptyIfNull();
IEnumerable<IConfigurationSection> safeChildren2 = cfg?.GetChildren().EmptyIfNull(); //Warning: Converting null literal or possible null value to non-nullable type.
}
</code>
<code> [return: NotNull]
public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T>? sourceEnumerable) => sourceEnumerable ?? Array.Empty<T>();
public static void ConfigurationSectionTest(IConfigurationSection? cfg)
{
IEnumerable<IConfigurationSection>? myChildren = cfg?.GetChildren();
IEnumerable<IConfigurationSection> safeChildren = myChildren.EmptyIfNull();
IEnumerable<IConfigurationSection> safeChildren2 = cfg?.GetChildren().EmptyIfNull(); //Warning: Converting null literal or possible null value to non-nullable type.
}
</code>
[return: NotNull]
public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T>? sourceEnumerable) => sourceEnumerable ?? Array.Empty<T>();
public static void ConfigurationSectionTest(IConfigurationSection? cfg)
{
IEnumerable<IConfigurationSection>? myChildren = cfg?.GetChildren();
IEnumerable<IConfigurationSection> safeChildren = myChildren.EmptyIfNull();
IEnumerable<IConfigurationSection> safeChildren2 = cfg?.GetChildren().EmptyIfNull(); //Warning: Converting null literal or possible null value to non-nullable type.
}