270

I am aware of two methods of casting types to IEnumerable from an Arraylist in Linq and wondering in which cases to use them?

e.g

IEnumerable<string> someCollection = arrayList.OfType<string>()

or

IEnumerable<string> someCollection = arrayList.Cast<string>()

What is the difference between these two methods and where should I apply each case?

riQQ
  • 9,878
  • 7
  • 49
  • 66

7 Answers7

404

OfType - return only the elements that can safely be cast to type x.
Cast - will try to cast all the elements into type x. if some of them are not from this type you will get InvalidCastException

EDIT
for example:

object[] objs = new object[] { "12345", 12 };
objs.Cast<string>().ToArray(); //throws InvalidCastException
objs.OfType<string>().ToArray(); //return { "12345" }
Dan Sinclair
  • 907
  • 1
  • 9
  • 27
Itay Karo
  • 17,924
  • 4
  • 40
  • 58
  • 1
    cheers for that. Tried both beforehand but both had elements all of the type expected hence why I could not see the difference. –  Oct 25 '10 at 15:24
  • 8
    @SLaks points out correctly that you should use `Cast` when you know for sure that the collection contains only type `T` elements. `OfType` is slower due to the `is` type check. If the collection is of type `IEnumerable`, `Cast` will simply cast the whole collection as `IEnumerable` and avoid enumerating it; `OfType` will still enumerate. ref: http://stackoverflow.com/questions/11430570/why-is-oftype-faster-than-cast – hIpPy Jul 11 '13 at 00:41
  • 43
    Even in cases where `.Cast()` does not throw when it is enumerated, it **is not** equivalent to `.OfType()`. The reason is that **`null` values** are always skipped by `.OfType()`. An example: `new System.Collections.ArrayList { "abc", "def", null, "ghi", }.OfType().Count()` will give only `3`; the similar expression with `.Cast()` evaluates to `4`. – Jeppe Stig Nielsen Dec 11 '13 at 12:08
  • 2
    in other words, it's something like the difference between 'as' and 'cast' operators – faza Feb 20 '19 at 11:21
138

Source: LINQ Tip: Enumerable.OfType - Solutionizing .NET

Fundamentally, Cast<T>() is implemented like this:

public IEnumerable<T> Cast<T>(this IEnumerable source)
{
  foreach(object o in source)
    yield return (T) o;
}

Using an explicit cast performs well, but will result in an InvalidCastException if the cast fails. A less efficient yet useful variation on this idea is OfType<T>():

public IEnumerable<T> OfType<T>(this IEnumerable source)
{
  foreach(object o in source)
    if(o is T t)
      yield return t;
}

The returned enumeration will only include elements that can safely be cast to the specified type.

NotAPro
  • 136
  • 2
  • 17
Ash
  • 1,924
  • 1
  • 13
  • 14
  • I have provided an explicit operator, yet it still fails. Does the argument have to be of type object? My operator expects an argument of type T, T is the type of elements in the given IEnumerable. – Ahmad Masalha Aug 17 '22 at 15:23
49

You should call Cast<string>() if you know that all of the items are strings.
If some of them aren't strings, you'll get an exception.

You should call OfType<string>() if you know that some of the items aren't strings and you don't want those items.
If some of them aren't strings, they won't be in the new IEnumerable<string>.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 2
    This answer is (currently) the only one that gives an explicit advice when to use which method. – CodeFox Aug 18 '16 at 11:46
  • 3
    Note that `OfType()` will also skip `null` values. e.g: `new string[] { "abc", "123", null, "" }.OfType().Count()` equals `3`, whereas `.Cast().Count()` would equal `4`. – baltermia Apr 27 '21 at 12:29
8

It should be noted that Cast(Of T) can be used on IEnumerable unlike other LINQ functions, so if there's ever a case where you need to use LINQ on a non-generic collection or list such as an ArrayList, you can use Cast(Of T) to cast to an IEnumerable(Of T) where LINQ can work.

T.S.
  • 18,195
  • 11
  • 58
  • 78
Niya
  • 243
  • 2
  • 8
2

Cast() will try to cast all elements of the collection (and will throw an exception if element is of the wrong type) while OfType() will return only elements of proper type.

Andrew Bezzub
  • 15,744
  • 7
  • 51
  • 73
2

OfType will filter the elements to return only the ones of the specified type. Cast will crash if an element cannot be cast to the target type.

T.S.
  • 18,195
  • 11
  • 58
  • 78
Johann Blais
  • 9,389
  • 6
  • 45
  • 65
2

Cast<T> will try to cast all items to the given type T. This cast could fail or throw an exception. OfType<T> will return a subset of the original collection and return only objects that are of type T.

T.S.
  • 18,195
  • 11
  • 58
  • 78
Brian Ensink
  • 11,092
  • 3
  • 50
  • 63