ich hab eine Verständnis frage:
Ursprünglich wollte ich folgendes machen:
List<uint> l1 = new List<uint> { 1, 2, 3 };
var l2 = l1.Cast<int>().ToList();
Das wirft
Fehler |
System.InvalidCastException: "Unable to cast object of type 'System.UInt32' to type 'System.Int32'." |
Nun ja, das lässt sich ja einfach mit
var l2 = l1.Select(i => (int)i).ToList();
Aber irgenwie hat mir das keine Ruhe gelassen, IEnumerable<T> Cast<T> ist folgendermaßen implementiert:
public static IEnumerable<T> Cast<T>(this IEnumerable source)
{
foreach (object o in source)
yield return (T)o;
}
uint i = 42;
object o = i;
int j = (int)o;
int i = 42;
object o = i;
int j = (int)o;