.NET DateTime has no predefined size

Since DateTime is a structure with members that seem to be broken down into simple mathematical values, I'm not sure why using sizeof () on it creates a message in the question header.

+5
source share
3 answers

Since the CLR can only determine size at runtime ... One of the reasons for this is "padding" (platform dependent) ...

, , sizeof . Marshal.SizeOf, , , , , sizeof. Marshal.SizeOf , sizeof , , .

.

. , ?

+7

, , :

CS0233: "System.DateTime" , sizeof ( System.Runtime.InteropServices.Marshal.SizeOf)

, unsafe ( # "", "" " ", ), :

    static void Main()
    {
        int s;
        unsafe
        {
            s = sizeof(DateTime);
        }
        Console.WriteLine(s); // writes 8
    }

unsafe sizeof() enum struct, ( DateTime - , ).

unsafe sizeof. ( , # 2 int enum, , DateTime, .)


, DateTime , Marshal.SizeOf<DateTime>() ( Marshal.SizeOf(typeof(DateTime)) .NET 4.5.1 (2013)) . ( struct) "".

+1

DateTime ( ).

0

All Articles