Why does the Count() method use the "checked" keyword?
Asked Answered
A

1

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;
    }
}
Amphitheater answered 25/3, 2020 at 13:4 Comment(1)
.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, note that they use uint.Chammy
J
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.

Juanajuanita answered 25/3, 2020 at 13:5 Comment(4)
@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 uncheckedJuanajuanita
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."Monkeypot
@DavidMårtensson ah, yes - good nuance; I was talking about runtime; as you say: compile-time is differentJuanajuanita
But compile time does not apply for the example of the post so my comment was incorrect and I have deleted it.Monkeypot

© 2022 - 2024 — McMap. All rights reserved.