Finding the size of DateTime in C#. How to?

Hi there;

I am trying to find the size of DateTime object, but couldn't find out

I am just creating a DateTime object and passing,

DateTime dt = new DateTime(2011, 4, 17, 21, 2, 12);

Can you provide me a code to find out the size of this object?

Kind regards.
LVL 12
jazzIIIloveAsked:
Who is Participating?
 
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
Bad assumption - they could do BUT there is absolutely no requirement.

Best place to check is the help files - that information should be contained there.  If it isn't then you would need to compare compare contents, specifically differences between different values contained in the same type of object.
0
 
Neil RussellTechnical Development LeadCommented:
You use SizeOf()

size = sizeof(dt)
0
 
jazzIIIloveAuthor Commented:
It gives error?

DateTime dt = new DateTime(2011, 4, 17, 21, 2, 12);
long size = sizeof(dt); //Type dt cannot be found...

what am i doing wrong?

Regards.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
AndyAinscowFreelance programmer / ConsultantCommented:
Why.
DateTime has methods - the answer you get is probably not what you want.
0
 
jazzIIIloveAuthor Commented:
It gives a compile error. Could you provide the example code of yours?

Regards.
0
 
Neil RussellTechnical Development LeadCommented:
Silly question but WHY do you want the size of the datetime? Is it so you can serialize it? Output on a stream?
0
 
mcs0506Connect With a Mentor Commented:
Hi,
Through Memory stream we can find out its size
Here is the sample code

            DateTime dateTime = DateTime.Now;
            byte[] bytes;
            MemoryStream memoryStream = new MemoryStream();
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            formatter.Serialize(memoryStream, dateTime);
            bytes = memoryStream.GetBuffer();
            long size = memoryStream.Length;
           

Happy coding

Dani
0
 
Neil RussellTechnical Development LeadCommented:
But that is NOT the size of a DateTime object in memory, Hence my question WHY?
0
 
jazzIIIloveAuthor Commented:
yep,

The reason is that I am comparing time_t of C++ with DateTime of C# (check for my other question regarding C++).

I just need to have 4 or 8 bytes or whatsoever for the C# DateTime.

mcs0506, your code's output is that;

Current time_t: 1303074132
Size of date time 78

I also go for the following code but again compile error in size+=sizeof(Value); line.

      public static int GetSizeOfObject(object obj)
        {
            object Value = null;
            int size = 0;
            Type type = obj.GetType();
            PropertyInfo[] info = type.GetProperties();
            foreach (PropertyInfo property in info)
            {
                Value = property.GetValue(obj, null);
               
                    size += sizeof(Value);                
            }
            return size;
        }

Regards.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
>>Hence my question WHY?

cough, cough.  comment #37043857
0
 
jazzIIIloveAuthor Commented:
Just comparison of sizes...I grab the C++ time_t size and I need C# DateTime one.

Regards.
0
 
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
>>I grab the C++ time_t size and I need C# DateTime one.
The question doesn't actually make any sense

That is chalk and cheese.  time_t is a 'native' variable such as int, double.  DateTime is a struct (class) with functions such as constructor... and, as such, rather different in size.

Why do you need to compare the sizes anyway, they have nothing to do with the contents.  1 takes up as much memory space as 42... (so long both are int, long, double, time_t....) and the size would be the same but the contents very different.
0
 
mcs0506Connect With a Mentor Commented:
Hi
try this one

using System;
namespace DateTimeSizeExample
{
  public struct TypeSizeProxy<T>
  {
    public T PublicField;
  }

  public static class SizeCalculator
  {
    public static int SizeOf<T>()
    {
    try
    {
      return System.Runtime.InteropServices.Marshal.SizeOf(typeof(T));
    }
    catch (ArgumentException)
    {
      return System.Runtime.InteropServices.Marshal.SizeOf(new TypeSizeProxy<T>());
    }
  }

  public static int GetSize(this object obj)
  {
    return System.Runtime.InteropServices.Marshal.SizeOf(obj);
  }
}

internal class Program
{
private static void Main(string[] args)
{

  int dateTimeSize = SizeCalculator.SizeOf<DateTime>(); // 8 bytes
}
}
}



Happy Coding

Dani
0
 
jazzIIIloveAuthor Commented:
ok mcs0506, thanks; but I have a question; AndyAinscow seems right,

>>time_t is a 'native' variable such as int, double.  DateTime is a struct (class) with functions such as >>constructor

Can you explain your code within the light of AndyAinscow comment.

Regards.
0
 
mcs0506Connect With a Mentor Commented:
yep AndyAinscow is right its not the native but by the help of generic types we can find out the memory consumption in term of Bytes of any object. This what that i have done in the code.

Dani
0
 
jazzIIIloveAuthor Commented:
Hmm, so it's 8 bytes just as time_t of C++.

I see, so, does this depend on the architecture of the system?

Regards.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
I am still puzzled by you wanting to compare the size.  Do you assume that objects with the same size will have the same granularity (accuracy) for the data contained within  them?
0
 
mcs0506Connect With a Mentor Commented:
Hi
 i have  checked it on 64 bit machine it returns the same result as 8 Bytes. One more thing (just for information nothing else) that in MS Excel you can convert datetime value to integer check out this integer is a long number which contains 8 Bytes on memory.

Dani
0
 
jazzIIIloveAuthor Commented:
AndyAinscow:
>> Do you assume that objects with the same size will have the same granularity (accuracy) for the data contained within  them?

Perfect question, in fact, yes!

But not possible?

mcs0506:
Thanks mvs0506.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.