Code:
object obj = new List<object>() { "a", "b" };
List<string> list = (List<string>)obj;
Exception:
System.InvalidCastException
HResult=0x80004002
Message=Unable to cast object of type 'System.Collections.Generic.List`1[System.Object]' to type 'System.Collections.Generic.List`1[System.String]'.
Source=ConsoleApp2
StackTrace:
at ConsoleApp2.Program.Main(String[] args) in
C:\Users\guoswang\source\repos\ConsoleApp2\ConsoleApp2\Program.cs:line 12
I know how to avoid this, but want to understand the root cause.