LINQ Cast() Method

  Total Views : 263
  Comments : 0
 
  Keywords : How to use linq cast() method / conversion operator with example, Linq cast() operator arraylist to ienumerable list with example in c# / vb.net, Linq cast operator to cast given list item

Here we will learn LINQ Cast() method with example in c#, vb.net and how to use LINQ cast() method to cast type or convert arraylist to IEnumerable list or string to int in c#, vb.net with example.

LINQ Cast Conversion Operator

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 type of elements (string / integer) in collection then the conversion will fail and it will throw an exception.

Syntax of LINQ Cast Conversion Operator

Following is the syntax of using LINQ Cast() operator to convert items in collection to another type of new collection.

 

C# Code

 

IEnumerable<string> result = obj.Cast<string>();

VB.NET Code

 

Dim result As IEnumerable(Of String) = obj.Cast(Of String)()

If you observe above syntax we are type casting “result” collection to new string object.

LINQ Cast Conversion Operator Example

Following is the example to cast or convert list / collection items into specified to data type of new collection.

 

C# Code

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Collections;

 

namespace LINQExamples

{

class Program

{

static void Main(string[] args)

{

ArrayList obj = new ArrayList();

obj.Add("India");

obj.Add("USA");

obj.Add("UK");

obj.Add("Australia");

IEnumerable<string> result = obj.Cast<string>();

foreach (var item in result)

{

Console.WriteLine(item);

}

Console.ReadLine();

}

}

}

VB.NET Code

 

Module Module1

Sub Main()

Dim obj As New ArrayList()

obj.Add("India")

obj.Add("USA")

obj.Add("UK")

obj.Add("Australia")

Dim result As IEnumerable(Of String) = obj.Cast(Of String)()

For Each item In result

Console.WriteLine(item)

Next

Console.ReadLine()

End Sub

End Module

If you observe above example we have an Arraylist in which we added few countries. These countries are of type Object and by using Cast operator we are converting ArrayList object to string type object.

Output of LINQ Cast Operator Example

Following is the result of LINQ Cast coversion operator example.

 

India

USA

UK

Australia

As we discussed Cast operator in LINQ will convert same type of elements present in one list to another new type in case if the collection contains different type of elements and if we try to convert that collection we will get error like “Unable to cast object of type ‘System.Int32’ to type ‘System.String’”.

 

C# Code

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Collections;

 

namespace LINQExamples

{

class Program

{

static void Main(string[] args)

{

ArrayList obj = new ArrayList();

obj.Add("India");

obj.Add("USA");

obj.Add("UK");

obj.Add("Australia");

obj.Add(1);

IEnumerable<string> result = obj.Cast<string>();

foreach (var item in result)

{

Console.WriteLine(item);

}

Console.ReadLine();

}

}

}

VB.NET Code

 

Module Module1

Sub Main()

Dim obj As New ArrayList()

obj.Add("India")

obj.Add("USA")

obj.Add("UK")

obj.Add("Australia")

obj.Add(1)

Dim result As IEnumerable(Of String) = obj.Cast(Of String)()

For Each item In result

Console.WriteLine(item)

Next

Console.ReadLine()

End Sub

End Module

If you observe above code we added an integer value at the end. Here we used Cast operator to cast all the values to string but when it reaches the integer value it will throw error like as shown below

 

LINQ Unable to cast object of type ‘System.Int32’ to type ‘System.String' Error

 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User