24

As I was looking the difference between Count and Count(), I thought to glance at the source code of Count(). I saw the following code snippet in which I wonder why the checked keyword is necessary/needed:

int num = 0;
using (IEnumerator<TSource> enumerator = source.GetEnumerator())
{
    while (enumerator.MoveNext())
    {
        num = checked(num + 1);
    }
    return num;
}

The source code:

// System.Linq.Enumerable
using System.Collections;
using System.Collections.Generic;

public static int Count<TSource>(this IEnumerable<TSource> source)
{
    if (source == null)
    {
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
    }
    ICollection<TSource> collection = source as ICollection<TSource>;
    if (collection != null)
    {
        return collection.Count;
    }
    IIListProvider<TSource> iIListProvider = source as IIListProvider<TSource>;
    if (iIListProvider != null)
    {
        return iIListProvider.GetCount(onlyIfCheap: false);
    }
    ICollection collection2 = source as ICollection;
    if (collection2 != null)
    {
        return collection2.Count;
    }
    int num = 0;
    using (IEnumerator<TSource> enumerator = source.GetEnumerator())
    {
        while (enumerator.MoveNext())
        {
            num = checked(num + 1);
        }
        return num;
    }
}
Boann
  • 48,794
  • 16
  • 117
  • 146
  • 2
    .NET 4.0 did not have this check yet, 4.5 did. Makes it somewhat likely that this was done to avoid trouble with [WinRT iterators](https://learn.microsoft.com/en-us/uwp/api/windows.foundation.collections.ivectorview_t_), note that they use uint. – Hans Passant Mar 25 '20 at 18:54

1 Answers1

36

Because it doesn't want to return a negative number in the (admittedly unlikely) event that there are more than 2-billion-odd items in the sequence - or a non-negative but just wrong number in the (even more unlikely) case that there are more than 4-billion-odd items in the sequence. checked will detect the overflow condition.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    @DavidMårtensson C# defaults to `unchecked`; it can be flipped to `checked` by default at the global level via a compiler switch, though - but frankly I rarely see that used, so I think it is very wrong to suggest that C# is "usually" run in `checked` mode; note also that `unsafe` has no interaction with `unchecked` – Marc Gravell Apr 01 '20 at 09:43
  • 1
    That was news to me, I tested this in a project before writing and C# complained about overflow until I added unchecked around it? Edit: Found the answer to what I saw "For constant expressions (expressions that can be fully evaluated at compile time), the default context is always checked. Unless a constant expression is explicitly placed in an unchecked context, overflows that occur during the compile-time evaluation of the expression cause compile-time errors." – David Mårtensson Apr 02 '20 at 16:22
  • @DavidMårtensson ah, yes - good nuance; I was talking about runtime; as you say: compile-time is different – Marc Gravell Apr 02 '20 at 16:52
  • But compile time does not apply for the example of the post so my comment was incorrect and I have deleted it. – David Mårtensson Apr 02 '20 at 23:53