1 out of 2 rated this helpful - Rate this topic

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:

// Constant value 4:
int intSize = sizeof(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.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Compile Time difference
It should also be noted the sizeof operator is evaluated at compile time.

For example: int intSize = sizeof(int) becomes int intSize = 4; at compile time.
Obtaining the size-of a type object.
You can't use the sizeof operator on a type object, nor on a generic type variable. Marshal.SizeOf() can be used, but it's not the same thing: it's the size after marshalling. Here's a function to obtain the size of a struct given its Type object:

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.
This is the really (not) just Marshal.SizeOf again
This item was in error. The sizeof() operator works as advertized during program execution, but not when using in a watch window.

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.
Obtaining the in-memory size of a structure
This item was in error: sizeof() works fine during program execution, but produces incorrect results when used in the debugger watch window.