Cast Operator with Anonymous Types
Below is a sample sourecode that demonstrates the usage of the Cast Operator when using the anonymous type
Cast Operator with Anonymous Types
C#
x
35
35
1
using System;
2
3
using System.Collections.Generic;
4
5
using System.Data;
6
7
using System.Linq;
8
9
namespace AbundantCode
10
11
{
12
13
internal class Program
14
15
{
16
17
//Cast Operator with Anonymous Types
18
19
private static void Main(string[] args)
20
21
{
22
23
var NumericValues = new object[] { "AbundantCode", 1, "765" };
24
25
foreach (var Number in NumericValues.Cast<int>())
26
27
Console.WriteLine(Number);
28
29
Console.ReadLine();
30
31
}
32
33
}
34
35
}
When the element within the List cannot be converted to the target type, you will receive an exception “InvalidCastException”.
1 Comment
Where is the solution for above problem