Back to: LINQ Tutorial For Beginners and Professionals
LINQ Cast Method in C# with Examples:
In this article, I am going to discuss LINQ Cast Method in C# with Examples. Please read our previous article where we discussed the LINQ ToDictionary Method in C# with Examples. Like the LINQ ToList, ToArray, and ToDictionary, the Cast Method also belongs to the conversion operators category.
LINQ Cast Method in C#:
The LINQ Cast Method in C# is used to casts all the elements of a collection (System.Collections.IEnumerable) to a specified type and then return a new System.Collections.Generic.IEnumerable<T> collection which contains all the elements of the source sequence cast to the specified type. This method uses deferred execution. The signature of this method is given below.
Parameters:
- source: The System.Collections.IEnumerable that contains the elements to be cast to type TResult.
Type Parameters:
- TResult: The type to cast the elements of the source.
Exceptions:
- It will throw the System.ArgumentNullException when the source is null.
- It will throw the System.InvalidCastException when an element in the source sequence cannot be cast to the specified type TResult.
Returns:
- It returns a System.Collections.Generic.IEnumerable contains each element of the source sequence cast to the specified type.
Example to Understand LINQ Cast Method in C#:
In the following example, the source sequence contains 3 elements and all these three elements are of type integer. So, all these elements are cast into integers. So, while casting using the Cast method, we need to specify the type to which we want to cast.
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 }; IEnumerable<int> result = list.Cast<int>(); foreach (int i in result) { Console.WriteLine(i); } Console.ReadKey(); } } }
Output: 10 20 30
Example2:
In the following example, we have created a non-generic (ArrayList) collection that contains integer and string values. Then we are casting that collection to IEnumerable<int> using the Cast<int> method. At runtime, while casting the values, it will throw System.InvalidCastException runtime exception when it tries to cast the string value.
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, }; //The following statement throws System.InvalidCastException list.Add("40"); IEnumerable<int> result = list.Cast<int>(); foreach (int i in result) { Console.WriteLine(i); } Console.ReadKey(); } } }
Example3:
In the following example, the source sequence is null. So, when we run the application, it will throw the System.ArgumentNullException.
using System.Linq; using System.Collections.Generic; using System; using System.Collections; namespace ConversionOperators { class Program { public static void Main() { ArrayList list = null; //Throws System.ArgumentNullException IEnumerable<int> result = list.Cast<int>(); foreach (int i in result) { Console.WriteLine(i); } Console.ReadKey(); } } }
In the next article, I am going to discuss the differences between Cast and OfType Methods in C# with Examples. Here, in this article, I try to explain the need and use of the LINQ Cast Method in C# with Examples. I hope you understood the need, and use of the Cast Method in C#.