なぜカウント()メソッドは、キーワードを「確認」を使用していますか?

SNR:

私が探していたとしてカウントし、カウントの違いを() 私は、ソースコードで一目に思いましたCount()私はなぜだろれる次のコードスニペットを見たcheckedキーワードが必要/必要です。

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

ソースコード:

// 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;
    }
}
マルクGravell:

または非陰性であるが-それは、順番に2つ以上の-億余りの項目があることを(確かにそう)イベントに負の数を返すように望んでいないので、単に間違っている(さらに多くの可能性は低い)場合、数シーケンス以上の4億余りの項目があること。checkedオーバーフロー状態を検出します。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=369358&siteId=1