-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumerableCast.cs
More file actions
26 lines (23 loc) · 808 Bytes
/
Copy pathEnumerableCast.cs
File metadata and controls
26 lines (23 loc) · 808 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public static class Program {
private static void PrintDoubles (IEnumerable<double> doubles) {
foreach (double d in doubles)
Console.WriteLine(d);
}
public static void Main () {
// test #1: casting of IEnumerable containg doubles to IEnumerable<double> should work
var list = new ArrayList() { 1.23, 4.56, 7.89 };
PrintDoubles(list.Cast<double>());
// test #2: an exception should be thrown once the string element is reached
list.Add("foo");
try {
PrintDoubles(list.Cast<double>());
Console.WriteLine("Fail");
} catch (InvalidCastException) {
Console.WriteLine("Pass");
}
}
}