Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5843671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:11:12+00:00 2026-05-22T12:11:12+00:00

It seems that I can cast DateTime to object, so why can’t I cast

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:11:12+00:00Added an answer on May 22, 2026 at 12:11 pm

    Array covariance only applies to arrays of reference types. DateTime is a value type so you can’t assign a DateTime[] to an object[] variable. You’ll have to explicitly create an object array and copy the values over. In other words, create a new array instance of type object[].

    There are plenty of ways you can do this. A simple use of CopyTo() should be enough.

    DateTime[] x = new DateTime[] { ... };
    object[] y = new object[x.Length];
    x.CopyTo(y, 0);
    

    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.

    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);
        }
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Mysql has two types that can hold boolean data, bit and bool. Bit(1) seems
It seems that Visual Basic can not reference sheets according to user-modified sheet names.
It seems that all of the documentation I can find about OpenGL-ES says something
It seems that anyone can snoop on incoming/outgoing .NET web service SOAP messages just
It seems that I can't control the NSApp delegate from within a System Preferences
I'm trying to consume a webmethod but it seems that my application can't resolve
It seems that in the future, we can just use the non-beta iPhone SDK
How can I add an URL to the trusted site? It seems that there
every introduction and sample that I can find seems to use GLUT or some
I can't believe that the following statement seems to be still true So, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.