This documentation is archived and is not being maintained.

Compiler Error CS0233

sizeof can only be used in an unsafe context (consider using System.Runtime.InteropServices.Marshal.SizeOf)

The sizeof operator can only be used in an unsafe block.

The following sample generates CS0233:

// CS0233.cs
using System;
using System.Runtime.InteropServices;

public struct S
   public int a;

public class MyClass
   public static void Main()
      S myS = new S();
      Console.WriteLine(sizeof(myS));   // CS0233
      // try the following line instead
      // Console.WriteLine(Marshal.SizeOf(myS));