C
C#

How to let compiler know that List is not null?

How to let compiler know that List is not null?

Nnelsonprsousa11/21/2023
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(); 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!
No description
Nnelsonprsousa11/21/2023
Note: I know we could use myList!, but that's developer work instead of compiler actually knowing šŸ˜‡
TThinker11/21/2023
Where are you getting myList from? The compiler is likely inferring this for a reason
JJoschi11/21/2023
I think you are looking for the NotNullWhenAttribute This is how the .NET TryParse methods are implemented.
c#
// Determines whether a String represents true or false.
//
public static bool TryParse([NotNullWhen(true)] string? value, out bool result) =>
TryParse(value.AsSpan(), out result);
c#
// Determines whether a String represents true or false.
//
public static bool TryParse([NotNullWhen(true)] string? value, out bool result) =>
TryParse(value.AsSpan(), out result);
It says: If this method returns true this parameter won't be null.
TThinker11/21/2023
oh, read the question wrong, sorry. yeah this is correct
Nnelsonprsousa11/23/2023
That was it, thank you!

Looking for more? Join the community!

C
C#

How to let compiler know that List is not null?

Join Server
Want results from more Discord servers?
Add your server
Recommended Posts
āœ… How to Fix MSB6006 yasm.exe exited with code -1073741515 on Cosmos Kernelāœ… Issue during pushing a commit to GitHub from VS CodeHi friends, I'm working on a project using `VS Code`, I used `VS Code` to clon that project from myāœ… Need help understanding inheritance with constructorsI know about constructors partially (I'm used to Unity's MonoBehaviour so constructors are mostly suHow to get the data of an adapter using MQTTnetThis is the documentation: https://docs.service.sicon.eco/sicon.os/2023.19.x/adapter-protocol-smqtt Converting from a string to a generic method invocationLooking for design patterns to help solve this problem I’m having at work. The situation is that we Auto refresh inside windows formTeam , We are using windows form c#. Is there any library for auto refresh (need to few logic eveāœ… Fresh Install of Windows, I'm Getting Weird Errors (NETSDK1/NU1012, and DEP0700)Anyone know what might be happening here in my Maui Blazor Hybrid projects? For the older project object reference not set to instance of object on a static method callso im trying to use an opengl binding package (Veldrid.OpenGLBinding) but, when i call any of the meWeb API model classesI'm coding a webshop using ASP.NET Core Web API for the backend and Next.js for the frontend. I'm deInvalidate JWT-Tokens on logout and password change.I have an ASP.net Web api that handles user interaction. For authentification I use JWT Tokens. I reDynamically enable/disable a route in ASP.NET 7?Is there a way to dynamically enable/disable a route in ASP.NET 7? I'd like to be able to enable/disāœ… I keep getting Error CS5001 and CS0028, how do i get rid of themin CS5001 it says Program does not contain a static "Main" method suitable for an entry point and in