It seems that I can cast DateTime to object, so why can’t I cast array DateTime[] to object[]? I know this has something to do with value/reference types, but doesn’t boxing allow me to do this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Array covariance only applies to arrays of reference types.
DateTimeis a value type so you can’t assign aDateTime[]to anobject[]variable. You’ll have to explicitly create an object array and copy the values over. In other words, create a new array instance of typeobject[].There are plenty of ways you can do this. A simple use of
CopyTo()should be enough.I ran some tests. Probably not the best way to do it but it should give a good idea of what it would be with a proper profiler.
Specs:
Win7Pro x64, Core2Quad Q9550@2.83GHz, 4GiB DDR2 1066 (PC2-8500)
64-bit build (32-bit is roughly the same, just less memory overall)
Test1 Mem: 40086256/200087360 (160001104) Time: 444 (1230723) Test2 Mem: 40091352/200099272 (160007920) Time: 751 (2078001) Test3 Mem: 40091416/200099256 (160007840) Time: 800 (2213764) Test4 Mem: 40091480/200099256 (160007776) Time: 490 (1358326) Test5 Mem: 40091608/300762328 (260670720) Time: 1407 (3893922) Test6 Mem: 40091672/300762328 (260670656) Time: 756 (2092566) Test1 Mem: 40091736/200099184 (160007448) Time: 515 (1425098) Test2 Mem: 40091736/200099184 (160007448) Time: 868 (2404151) Test3 Mem: 40091736/200099160 (160007424) Time: 885 (2448850) Test4 Mem: 40091736/200099184 (160007448) Time: 540 (1494429) Test5 Mem: 40091736/300762240 (260670504) Time: 1479 (4093676) Test6 Mem: 40091736/300762216 (260670480) Time: 746 (2065095) Test1 Mem: 40091736/200099168 (160007432) Time: 500 (1383656) Test2 Mem: 40091736/200099160 (160007424) Time: 781 (2162711) Test3 Mem: 40091736/200099176 (160007440) Time: 793 (2194605) Test4 Mem: 40091736/200099184 (160007448) Time: 486 (1346549) Test5 Mem: 40091736/300762232 (260670496) Time: 1448 (4008145) Test6 Mem: 40091736/300762232 (260670496) Time: 749 (2075019) Test1 Mem: 40091736/200099184 (160007448) Time: 487 (1349320) Test2 Mem: 40091736/200099176 (160007440) Time: 781 (2162729) Test3 Mem: 40091736/200099184 (160007448) Time: 800 (2214766) Test4 Mem: 40091736/200099184 (160007448) Time: 506 (1400698) Test5 Mem: 40091736/300762224 (260670488) Time: 1436 (3975880) Test6 Mem: 40091736/300762232 (260670496) Time: 743 (2058002) Test1 Mem: 40091736/200099184 (160007448) Time: 482 (1335709) Test2 Mem: 40091736/200099184 (160007448) Time: 777 (2150719) Test3 Mem: 40091736/200099184 (160007448) Time: 793 (2196184) Test4 Mem: 40091736/200099184 (160007448) Time: 493 (1365222) Test5 Mem: 40091736/300762240 (260670504) Time: 1434 (3969530) Test6 Mem: 40091736/300762232 (260670496) Time: 746 (2064278)Interestingly,
ConvertAll()performs much the same as a plain loop.