© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
5 replies
Nelson Sousa

How to let compiler know that List is not null?

Hi community!

I have the following extension method:, which checks if the source is not null and if it contains any elements:
public static bool SafeAny<T>(this IEnumerable<T> source) => source != null && source.Any();
public static bool SafeAny<T>(this IEnumerable<T> source) => source != null && source.Any();


I'd like to inform the compiler that, when returning true, the source is not null. Is this possible to achieve with C#?

Thank you!
image.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

assure compiler type is not null
C#CC# / help
2y ago
❔ What could cause the compiler to not infer this nullability correctly
C#CC# / help
3y ago
✅ How to add data to a list that is outside of Main()?
C#CC# / help
3y ago
❔ selectedCar is null, I need it to not be null
C#CC# / help
3y ago