Difference between Cast and OfType Operators

Difference between Cast and OfType Operators in C#

In this article, I am going to discuss the difference between Cast and OfType Operators in C# with an example. This is one of the frequently asked interview questions. For a better understanding of this question, I strongly recommended you to read the following two articles before proceeding to this article.

  1. Cast Operator in C#
  2. OfType Operator in C#
Difference between Cast and OfType operators:

The Cast operator in C# will try to cast all the elements present in the source sequence into a specified type. If any of the elements in the source sequence cannot be cast to the specified type then it will throw InvalidCastException.

On the other hand, the OfType operator in C# returns only the elements of the specified type and the rest of the elements in the source sequence will be ignored as well as excluded from the result. 

Cast Operator Example:

In the following example, the Cast operator throws InvalidCastException as it is unable the Cast the value “50” (it is in the form of a string) to an integer.

using System.Linq;
using System.Collections.Generic;
using System;
using System.Collections;

namespace ConversionOperators
{
    class Program
    {
        public static void Main()
        {
            ArrayList list = new ArrayList
            {
                10,
                20,
                30,
                "50"
            };

            IEnumerable<int> result = list.Cast<int>();

            foreach (int i in result)
            {
                Console.WriteLine(i);
            }

            Console.ReadKey();
        }
    }
}

Now run the above application, it should give you the following exception.

Difference between Cast and OfType Operators in C#

OfType Operator Example:

Let us rewrite the same example using the OfType operator as shown below.

using System.Linq;
using System.Collections.Generic;
using System;
using System.Collections;

namespace ConversionOperators
{
    class Program
    {
        public static void Main()
        {
            ArrayList list = new ArrayList
            {
                10,
                20,
                30,
                "50"
            };
            
            IEnumerable<int> result = list.OfType<int>();

            foreach (int i in result)
            {
                Console.WriteLine(i);
            }

            Console.ReadKey();
        }
    }
}

Output: 10 20 30

Now, when you run the application, you will not get any exception. The value “50” is ignored as well as excluded from the result.

When to use Cast over OfType and vice versa?

We need to use the Cast Operator

  1. When we want to cast all the elements in the collection
  2. When we know for sure the collection contains only elements of the specified type

We need to use the OfType operator

  1. When we want to filter the elements and return only the specified type of elements. 

From the next article onwards, I am going to discuss the Parallel Linq in C# with examples. Here, in this article, I try to explain the difference between Cast and OfType Operators as well as we also discussed when to use Cast and OfType operators in C# with an example.

1 thought on “Difference between Cast and OfType Operators”

Leave a Reply

Your email address will not be published. Required fields are marked *