sizeof() operator for types

.net, c#

Solution

The `sizeof` operator in C# works only on compile-time known types, not on variables (instances).

The correct example would be

int variable = 10;
int sizeOfVariable = sizeof(int);

So probably you are looking for `Marshal.SizeOf` which can be used on any object instances or runtime types.

int variable = 10;
int sizeOfVariable = Marshal.SizeOf(variable);    

See here for more information

Problem

I would normally do this in my C++ code: ``` int variable = 10; int sizeOfVariable = sizeof(variable); //Returns 4 for 32-bit process ``` But that doesn't seem to work for C#. Is there an analog?

Original source

Related problems