sizeof (C# Reference)
Used to obtain the size in bytes for an unmanaged type. Unmanaged types include the built-in types that are listed in the table that follows, and also the following:
Enum types
Pointer types
User-defined structs that do not contain any fields or properties that are reference types
The following example shows how to retrieve the size of an int:
Starting with version 2.0 of C#, applying sizeof to built-in types no longer requires that unsafe mode be used.
The sizeof operator cannot be overloaded. The values returned by the sizeof operator are of type int. The following table shows the constant values that are substituted for sizeof expressions that have certain built-in types as operands.
|
Expression |
Constant value |
|---|---|
|
sizeof(sbyte) |
1 |
|
sizeof(byte) |
1 |
|
sizeof(short) |
2 |
|
sizeof(ushort) |
2 |
|
sizeof(int) |
4 |
|
sizeof(uint) |
4 |
|
sizeof(long) |
8 |
|
sizeof(ulong) |
8 |
|
sizeof(char) |
2 (Unicode) |
|
sizeof(float) |
4 |
|
sizeof(double) |
8 |
|
sizeof(decimal) |
16 |
|
sizeof(bool) |
1 |
For all other types, including structs, the sizeof operator can be used only in unsafe code blocks. Although you can use the Marshal.SizeOf method, the value returned by this method is not always the same as the value returned by sizeof. Marshal.SizeOf returns the size after the type has been marshaled, whereas sizeof returns the size as it has been allocated by the common language runtime, including any padding.
class MainClass
{
// unsafe not required for primitive types
static void Main()
{
Console.WriteLine("The size of short is {0}.", sizeof(short));
Console.WriteLine("The size of int is {0}.", sizeof(int));
Console.WriteLine("The size of long is {0}.", sizeof(long));
}
}
/*
Output:
The size of short is 2.
The size of int is 4.
The size of long is 8.
*/
For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.
For example: int intSize = sizeof(int) becomes int intSize = 4; at compile time.
- 10/22/2010
- dmex
using System;
using System.Reflection;
using System.Reflection.Emit;
...
// GetManagedSize() returns the size of a structure whose type
// is 'type', as stored in managed memory. For any referenec type
// this will simply return the size of a pointer (4 or 8).
public static int GetManagedSize(Type type)
{
// all this just to invoke one opcode with no arguments!
var method = new DynamicMethod("GetManagedSizeImpl",
typeof(uint), new Type[0],
typeof(TypeExtensions),
false);
ILGenerator gen = method.GetILGenerator();
gen.Emit(OpCodes.Sizeof, type);
gen.Emit(OpCodes.Ret);
var func = (Func<uint>)method.CreateDelegate(typeof(Func<uint>));
return checked((int)func());
}
This uses a dynamically generated method so you can invoke the MSIL sizeof opcode with any type.
It isn't always useful- the size returend for a reference type is always the size of a pointer, which is pretty useless. But you can use this on generic types: GetManagedSize(typeof(T)). If T is a structure type, you get its physical size in managed memory.
- 10/10/2010
- Daniel N. Johnson
The sizeof operator is not equivalent Marshal.SizeOf(); the operator returns the in-memory size of an item. When used in a VS2010 watch window, it returns the same value as Marshal.SizeOf().
That can be really confusing, but won't affect normal program execution.
- 10/10/2010
- Daniel N. Johnson
- 10/10/2010
- Daniel N. Johnson
- 10/10/2010
- Daniel N. Johnson
- 10/10/2010
- Daniel N. Johnson