1

データがデータベースまたはファイルに書き込まれているときに一時的にデータを追加して削除する構造体型のリストがありますlist.RemoveAll。メソッドを使用してリストから項目を削除しています。

list.RemoveAllカウントがゼロのリストで呼び出すとスローされるはずですArgumentNullExceptionが、カウントがゼロであっても例外はスローされません。

    _dataNotWrittenToDb.RemoveAll(item => item.ScopeId == _a2Data.ScopeId);
    _dataWrittenToDB.RemoveAll(item => item.ScopeId == _a2Data.ScopeId);
4

4 に答える 4

3

MSDNから

例外

Exception   Condition
ArgumentNullException   

match is null.

RemoveAll は、渡されたパラメーターが null の場合にのみ ArgumentNullException をスローします。あなたの場合は null ではありません

RemoveAll の仕事は、特定の条件に一致するすべての要素を削除することです。あなたの場合、一致する要素はなく、削除も、例外を返す理由もありません

于 2012-09-04T09:55:12.830 に答える
1

例外が必要な場合は、次のようにすることができます。

Predicate<YourItemStruct> p = item => item.ScopeId == _a2Data.ScopeId;

if (!_dataNotWrittenToDb.Exists(p))
  throw new Exception("No items exist to delete");
_dataNotWrittenToDb.RemoveAll(p); 
if (!_dataWrittenToDb.Exists(p))
  throw new Exception("No items exist to delete");
_dataWrittenToDb.RemoveAll(p); 
于 2012-09-04T10:09:47.320 に答える
0

逆コンパイルされたソースの一覧から:

  public int RemoveAll(Predicate<T> match)
    {
      if (match == null)
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
      int index1 = 0;
      while (index1 < this._size && !match(this._items[index1]))
        ++index1;
      if (index1 >= this._size)
        return 0;
      int index2 = index1 + 1;
      while (index2 < this._size)
      {
        while (index2 < this._size && match(this._items[index2]))
          ++index2;
        if (index2 < this._size)
          this._items[index1++] = this._items[index2++];
      }
      Array.Clear((Array) this._items, index1, this._size - index1);
      int num = this._size - index1;
      this._size = index1;
      ++this._version;
      return num;
    }

ご覧のとおり、このメソッドがスローされるのArgumentNullExceptionは、引数が実際に等しい場合のみですnull

于 2012-09-04T09:55:17.563 に答える
-1

が 0 の場合count、リストが空であることを意味します。しかし、リストは存在します。Nullリストが存在しないことを意味します。

NullReferenceExceptionnull空ではなく、オブジェクトに対してのみスローされます。

箱がまったくないのと比べて、実際の空の箱のようなものです。

于 2012-09-04T09:55:05.990 に答える