Cast Operator

Introduction:

In C#, the Cast operator is a commonly used operator for converting objects to a specific data type. The Cast operator is useful when you have a collection of objects that contains multiple types, and you need to convert it to a collection of a specific type. In this lesson, we will explore the Cast operator in detail, including its definition, syntax, and examples.

Definition:

The Cast operator is a LINQ extension method that is used to convert a collection of objects to a collection of a specified data type. The Cast operator works by checking each item in the collection to see if it can be cast to the specified type, and then casting it to that type. If an item cannot be cast to the specified type, an InvalidCastException is thrown.

Syntax:

The syntax for the Cast operator is as follows:

IEnumerable<TResult> Cast<TResult>(this IEnumerable source)

The Cast operator takes an IEnumerable as its parameter and returns an IEnumerable of the specified data type. The generic type parameter, TResult, specifies the data type to which the objects should be cast.

Example 1: Casting a List of Objects to a List of Strings

Let’s say we have a List of objects that contains strings, integers, and a boolean value, and we want to convert it to a List of strings. We can use the Cast operator to accomplish this as follows:

List<object> myObjects = new List<object>();
myObjects.Add("Hello");
myObjects.Add("World");
myObjects.Add(123);
myObjects.Add(true);

List<string> myStrings = myObjects.Cast<string>().ToList();

In this example, we first create a List of objects called myObjects. We then add four items to the list: two strings, an integer, and a boolean value. We then use the Cast operator to convert the list of objects to a list of strings. The Cast operator checks each item in the list to see if it can be cast to a string, and then casts it to a string. The result is a List of strings called myStrings.

Example 2: Casting an Array of Objects to an Array of Integers

Let’s say we have an array of objects that contains integers and strings, and we want to convert it to an array of integers. We can use the Cast operator to accomplish this as follows:

object[] myObjects = { 1, "2", 3, "4" };

int[] myIntegers = myObjects.Cast<int>().ToArray();

In this example, we first create an array of objects called myObjects. We then add four items to the array: two integers and two strings. We then use the Cast operator to convert the array of objects to an array of integers. The Cast operator checks each item in the array to see if it can be cast to an integer, and then casts it to an integer. The result is an array of integers called myIntegers.

Conclusion:

The Cast operator is a useful tool for converting a collection of objects to a specific data type in C#. It allows developers to work with collections of mixed types more easily and efficiently. By understanding how to use the Cast operator and its syntax, developers can take advantage of its benefits to streamline their code and improve its readability.