Which is better when check a list is null : not null or use Any

Rai Vu

Which is better when check list is null ?

var newList;

if(newList!= null)

or newList.Any()

In the above code, sometimes I check not null and sometimes I use Any(), I don't know which one is best practice and why ?

Any advice?

Thanks in advance

Pac0

These are not the same.

Any will throw an exception if used on a null reference.

With lists, you can think of .Any() as .Count() != 0 (*)

You may have to check for both, and you have to do the null check before calling Any() on your IEnumerable.

One way is to check them for both in one strike with the null-safe navigation ?.as in Thierry V's answer.

But sometimes you want to throw a custom Exception if you have a null value that you are not supposed to have, and treat an empty list as a correct input, so it all depends on the context.

Just remember that these are different.

(*) : as noticed in a comment, .Any() is not actually implemented as Count() == 0. For lists, it's functionally equivalent, but it's best practice to use Any() to test if an IEnumerable is empty or not, because Count() might need to go through all the elements.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

When to use NULL and when to use '\0' in linked list in C?

分類Dev

Why use null function instead of == [] to check for empty list in Haskell?

分類Dev

assert Vs if(var != null) which way is better or is correct?

分類Dev

Check list of values is NULL in PL/SQL

分類Dev

Null check vs try/catch when 99% of the time object is not null

分類Dev

Null check vs try/catch when 99% of the time object is not null

分類Dev

Check if a String is null or empty when it's null sometimes and sometimes not

分類Dev

How to check for NULL when mapping nested JSON?

分類Dev

Invalid use of Null Access VBA even when IIF condition states not to check

分類Dev

C# elegant way to check if all shallow properties are null (or any property is not null)

分類Dev

Check if CGRect null in getter

分類Dev

Check for NULL or empty '' in function?

分類Dev

If statement with a null check

分類Dev

Check, if a variable is null in a view

分類Dev

Check '!= null" not working

分類Dev

Conditional statement for null check

分類Dev

Check if null or empty in jolt and put another value which is present in input JSON

分類Dev

.blur() vs .on() , which is better to use ( currently )

分類Dev

getting null value in list when passing by ajax call to mvc controller

分類Dev

Null point exception when trying to add a List<String> to HashMap

分類Dev

Better way to check a list for specific elements - python

分類Dev

Error parameter specified as non-null is null when use swipe touch listener in recyclerView - Android

分類Dev

check null,empty or undefined angularjs

分類Dev

null check in try-with-resources

分類Dev

check null,empty or undefined angularjs

分類Dev

check null,empty or undefined angularjs

分類Dev

String append with Null Check Kotlin

分類Dev

How to check if 2 values IS NULL

分類Dev

How to check if NSDictionary key is NULL

Related 関連記事

  1. 1

    When to use NULL and when to use '\0' in linked list in C?

  2. 2

    Why use null function instead of == [] to check for empty list in Haskell?

  3. 3

    assert Vs if(var != null) which way is better or is correct?

  4. 4

    Check list of values is NULL in PL/SQL

  5. 5

    Null check vs try/catch when 99% of the time object is not null

  6. 6

    Null check vs try/catch when 99% of the time object is not null

  7. 7

    Check if a String is null or empty when it's null sometimes and sometimes not

  8. 8

    How to check for NULL when mapping nested JSON?

  9. 9

    Invalid use of Null Access VBA even when IIF condition states not to check

  10. 10

    C# elegant way to check if all shallow properties are null (or any property is not null)

  11. 11

    Check if CGRect null in getter

  12. 12

    Check for NULL or empty '' in function?

  13. 13

    If statement with a null check

  14. 14

    Check, if a variable is null in a view

  15. 15

    Check '!= null" not working

  16. 16

    Conditional statement for null check

  17. 17

    Check if null or empty in jolt and put another value which is present in input JSON

  18. 18

    .blur() vs .on() , which is better to use ( currently )

  19. 19

    getting null value in list when passing by ajax call to mvc controller

  20. 20

    Null point exception when trying to add a List<String> to HashMap

  21. 21

    Better way to check a list for specific elements - python

  22. 22

    Error parameter specified as non-null is null when use swipe touch listener in recyclerView - Android

  23. 23

    check null,empty or undefined angularjs

  24. 24

    null check in try-with-resources

  25. 25

    check null,empty or undefined angularjs

  26. 26

    check null,empty or undefined angularjs

  27. 27

    String append with Null Check Kotlin

  28. 28

    How to check if 2 values IS NULL

  29. 29

    How to check if NSDictionary key is NULL

ホットタグ

アーカイブ