sizeof(bool) quick question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi there,

i want to know size of managed bool (know it is 1byte according to language
spec)
but how do i do it in code?

Marshal.Sizeof... isn't an option cause it returns 4 bytes, i.e. unmanaged
Boolean

what is correct syntax?

Thanks in advance

Brian
 
You can use sizeof(bool) but then you need to compile with the /unsafe
switch
and also place that unsafe code in an unsafe code block.

unsafe
{
Console.WriteLine(sizeof(bool));
}

PS
Specifying to compile with the unsafe switch can also be done from the
VS.NET IDE
under Project->[ProjectName] Properties...->Configuration Properties->Build
 
Brian said:
Hi there,

i want to know size of managed bool (know it is 1byte according to
language spec)
but how do i do it in code?

Marshal.Sizeof... isn't an option cause it returns 4 bytes, i.e. unmanaged
Boolean

what is correct syntax?

You can use sizeof in C# but you have to specify the code as unsafe. Ex:

unsafe static int GetSize()
{
return sizeof(bool);
}

You will see that returns 1.
 
Thanks for your reply.


Dennis Myrén said:
You can use sizeof(bool) but then you need to compile with the /unsafe
switch
and also place that unsafe code in an unsafe code block.

unsafe
{
Console.WriteLine(sizeof(bool));
}

PS
Specifying to compile with the unsafe switch can also be done from the
VS.NET IDE
under Project->[ProjectName] Properties...->Configuration Properties->Build

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Brian Keating said:
Hi there,

i want to know size of managed bool (know it is 1byte according to
language
spec)
but how do i do it in code?

Marshal.Sizeof... isn't an option cause it returns 4 bytes, i.e. unmanaged
Boolean

what is correct syntax?

Thanks in advance

Brian
 
Thanks for your reply

Tom Porterfield said:
You can use sizeof in C# but you have to specify the code as unsafe. Ex:

unsafe static int GetSize()
{
return sizeof(bool);
}

You will see that returns 1.
 
Back
Top