Home » LINQ Cast Method

LINQ Cast Method

by Online Tutorials Library

LINQ Cast() Method

In LINQ, Cast operator is used to cast/convert all the elements present in a collection into a specified data type of new collection. In case if we try to cast/convert different types of elements (string/integer) in the collection, then the conversion will fail, and it will throw an exception.

Syntax of LINQ Cast () conversion operator

C# Code

Example of LINQ Cast Conversion Operator

Output:

LINQ Cast() Method

In the above example, we have an Arraylist where we added a few countries. These countries are a type of Object, and by using Cast operator, we are converting the ArrayList Object to String type object.


You may also like