How can I implement NotOfType in LINQ that has a nice calling syntax?

前端 未结 7 1196
轻奢々
轻奢々 2020-12-11 00:24

I\'m trying to come up with an implementation for NotOfType, which has a readable call syntax. NotOfType should be the complement to OfType&l

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-11 00:47

    This might seem like a strange suggestion, but what about an extension method on plain old IEnumerable? This would mirror the signature of OfType, and it would also eliminate the issue of the redundant type parameters.

    I would also argue that if you have a strongly-typed sequence already, there is very little reason for a special NotOfType method; it seems a lot more potentially useful (in my mind) to exclude a specific type from a sequence of arbitrary type... or let me put it this way: if you're dealing with an IEnumerable, it's trivial to call Where(x => !(x is T)); the usefulness of a method like NotOfType becomes more questionable in this case.

提交回复
热议问题