似乎我可以将DateTime转换为object,为什么我不能将数组DateTime []转换为object []?我知道这与值/引用类型有关,但拳击不允许我这样做吗?
答案 0 :(得分:13)
Array covariance仅适用于引用类型的数组。 DateTime
是一种值类型,因此您无法将DateTime[]
分配给object[]
变量。您必须显式创建一个对象数组并复制值。换句话说,创建一个类型为object[]
的新数组实例。
有很多方法可以做到这一点。简单地使用CopyTo()
就足够了。
DateTime[] x = new DateTime[] { ... };
object[] y = new object[x.Length];
x.CopyTo(y, 0);
class Program
{
static void Main(string[] args)
{
var now = DateTime.Now;
var dates = new DateTime[5000000];
for (int i = 0; i < dates.Length; i++)
dates[i] = now.AddSeconds(i);
for (int i = 0; i < 5; i++)
{
Test("Test1", () =>
{
var result = new object[dates.LongLength];
for (long l = 0; l < result.LongLength; l++)
result[l] = dates[l];
return result;
});
Test("Test2", () =>
{
var result = new object[dates.LongLength];
dates.CopyTo(result, 0);
return result;
});
Test("Test3", () =>
{
var result = new object[dates.LongLength];
Array.Copy(dates, result, dates.LongLength);
return result;
});
Test("Test4", () =>
{
var result = Array.ConvertAll(dates, d => (object)d);
return result;
});
Test("Test5", () =>
{
var result = dates.Cast<object>().ToArray();
return result;
});
Test("Test6", () =>
{
var result = dates.Select(d => (object)d).ToArray();
return result;
});
Console.WriteLine();
}
}
static void Test<T>(string name, Func<T> fn)
{
var startMem = GC.GetTotalMemory(true);
var sw = Stopwatch.StartNew();
var result = fn();
sw.Stop();
var endMem = GC.GetTotalMemory(false);
var diff = endMem - startMem;
Console.WriteLine("{0}\tMem: {1,7}/{2,7} ({3,7})", name, startMem, endMem, diff);
Console.WriteLine("\tTime: {0,7} ({1,7})", sw.ElapsedMilliseconds, sw.ElapsedTicks);
}
}
规格:
Win7Pro x64,Core2Quad Q9550@2.83GHz,4GiB DDR2 1066(PC2-8500)
64位构建(32位大致相同,总体内存更少)
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)
有趣的是,ConvertAll()
的表现与普通循环非常相似。
答案 1 :(得分:4)
您无法将DateTime[]
投射到object[]
因为它不安全。所有相同长度的引用类型数组在内存中具有相同的布局。 DateTime是值类型,数组是“flat”(未装箱)。您无法安全地转换为object[]
,因为内存中的布局与object[]
不兼容。
答案 2 :(得分:3)
如果你有LINQ可用(.NET 3.5+),你可以这样做:
DateTime[] dates = new DateTime[3];
dates[0] = new DateTime(2009, 01, 01);
dates[1] = new DateTime(2010, 01, 01);
dates[2] = new DateTime(2011, 01, 01);
object[] dates2 = Array.ConvertAll(dates, d => (object)d);
正如Jeff指出的那样,你也可以使用委托在.NET 2.0中做类似的事情:
object[] dates3 = Array.ConvertAll(dates,
delegate(DateTime d) { return (object)d; });
答案 3 :(得分:0)
顺便说一句,您可以使用Array.Copy()
完成此操作void Main()
{
DateTime[] dates = new DateTime[] { new DateTime(2000, 1, 1), new DateTime (2000, 3, 25) };
object[] objDates = new object[2];
Array.Copy(dates, objDates, 2);
foreach (object o in objDates) {
Console.WriteLine(o);
}
}
答案 4 :(得分:0)
由于DateTime
是object
,但DateTime
的数组不是 {{1}的数组}。
值类型的数组与引用类型的数组不同,因此这两种类型的数组从根本上是不兼容的。值类型数组实际上包含值,而引用类型数组仅包含引用。
答案 5 :(得分:0)
请参阅 why 的其他答案,但不能这样做。
另一种方法是执行数组的深层复制。使用LINQ的示例:
DateTime[] dates = ...;
object[] objects = dates.Select(d => (object)d).ToArray();