sizeof(T) where T : struct

I have been messing around with this nice face recognition API, and just got thinking on one code line in the samples retrieving the size of a struct.

thesize =  sizeof(double) + 3*sizeof(int)

Why on earth would anyone do this? I've been coding in C# for 5-6yrs, however, I have never ever needed the sizeof operator. Have you ever?
So I went the straightforward sizeof way.

I got a nice error:
struct does not have a predefined size, therefore sizeof can only be used in an unsafe context (consider using System.Runtime.InteropServices.Marshal.SizeOf)

I have found an excellent post on how to retrieve the size of a structure on informit.

No comments:

Post a Comment