-4

This is my Code:

public int PostCanal(List<int> listchannel) {

if (listchannel == null || listchannel.Contains(0))
{
    listchannel.Add(1);
}

This list has values coming from a checkbox menu. So, if the user uncheck all the options I want to still using "1" as default value.

On this case, the listchannel List<int> is arriving with [0] value. However the if condition is skipped.

Any idea? Also tried .Equals Method.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Citrusl
  • 11
  • 1
  • 2
  • 7
  • 3
    Does this answer your question? [Check if list is empty in C#](https://stackoverflow.com/questions/18867180/check-if-list-is-empty-in-c-sharp) – V0ldek Jan 02 '20 at 23:02
  • 2
    Can you please clarify if you want to check if "list is empty" (no elements, as @V0ldek suggested) or "list has `0`" (as code shown in the post tires to check)? Some real [MCVE] with sample data as part of the code would help... – Alexei Levenkov Jan 02 '20 at 23:04
  • 1
    There's a different between an empty list and a list containing a single value `0`. You are checking for the latter. To check for the former, use `list.Count == 0` or `!list.Any()`. – Rotem Jan 02 '20 at 23:05
  • Note that it is considered by many to be a bad practice to treat a null list the same as an empty list, as you are attempting to do here. The better practice is to make a null list *illegal* by throwing a "bad argument" exception; if you train the caller to never pass a null list by crashing, then they stop passing null lists. – Eric Lippert Jan 02 '20 at 23:28
  • I notice also that you seem to be both mutating a list and returning a value; that's a code smell. Ideally we like methods that are useful for their effects or useful for their values but not both. (Because if you want the value, then you are forced to get the effect.) Can you say a bit about what your method is actually doing? There might be a better way to structure this code. – Eric Lippert Jan 02 '20 at 23:30
  • @Rotem: `Contains(0)` does not check to see if a list contains a *single* value zero. It checks to see if the list contains *one or more* zero values. To check whether a list contains exactly one zero you'd use something like `list.Where(x => x == 0).Take(2).Count() == 1` – Eric Lippert Jan 02 '20 at 23:32
  • I note also that you seem to have the condition backwards. If the list is null then you are adding an element to it, which will crash. Again, more clearly say what you are trying to do here, because your code seems like it is not doing what you want. – Eric Lippert Jan 02 '20 at 23:36
  • @EricLippert Hey thanks! For some reason it had never occurred to me to use `Take()` before `Count()`. I need it for the opposite reason than you're using it here, but your snippet gave me the idea to use `.Take(n).Count() == n` instead of my `AtLeast(n)` extension method that I created kind of as a cross between `.Any()` and `.Count()` to avoid iterating through the entire enumerable when I only care if there are at least n items. Thanks! – itsme86 Jan 03 '20 at 23:28
  • @itsme86: It's a handy technique; glad I could help. – Eric Lippert Jan 03 '20 at 23:33

1 Answers1

-1

You could use Any Or Count to check if the list is empty or not, like the following code :

if(listchannel == null || !listchannel.Any())
{
}

//Or 
if(listchannel == null || listchannel.Count == 0)
{
}

I hope you find this helpful.

Mohammed Sajid
  • 4,778
  • 2
  • 15
  • 20
  • Generally you should prefer the `.Any()` extension method over checking the `.Count` property for zero. The Any call will call the enumerator exactly once (if the enumerator returns an item, Any returns `true`, if the enumerator returns the indication that there are no items, Any returns `false`). The Count property requires that the entire collection be enumerated (unless the collection keeps track of the count) – Flydog57 Jan 03 '20 at 00:01
  • 3
    @Flydog57: The `Count` **property** on a list knows the size of the list and is O(1) to call. You are confusing the `Count` **property** on a list with the `Count()` extension method. The extension method checks to see if the sequence knows its own length, and if so, returns that value; if not, then it enumerates to determine the size of the list. **It is always efficient to count a list**. – Eric Lippert Jan 03 '20 at 19:19
  • 1
    @Flydog57: That said, I agree with the thrust of your comment but for a different reason. We prefer `Any` to `Count` because it more clearly matches the semantics of the operation the user intends to perform, namely, checking to see if there are any elements in a sequence. – Eric Lippert Jan 03 '20 at 19:21
  • @Flydog57 not just that, consider the Law of Demeter as well. – MeTitus Feb 16 '21 at 12:10