
Every value type uses a certain number of bytes. The sizeof operator returns this number in the C# language. Due to the virtualized type layout, this operator is limited. It can only compute the byte size of value types. Because of its limitations, the sizeof operator is often used incorrectly and often results in errors.
KeywordsThis C# article shows how to use the sizeof operator. Sizeof returns the number of bytes.

This example program demonstrates the evaluation of the sizeof operator in the C# language. It uses many local variables and assigns these locals to the result of sizeof expressions. The sizeof operator is evaluated at compile-time, and when you execute this program, it will just use constant numbers. The program does not compute the size of reference types such as string or array, because this is not possible.
Program that uses sizeof [C#]
using System;
class Program
{
static void Main()
{
//
// Evaluate the size of some value types.
// ... Invalid sizeof expressions are commented out here.
// ... The results are integers printed on separate lines.
//
int size1 = sizeof(int);
int size2 = 0; // sizeof(int[]);
int size3 = 0; // sizeof(string);
int size4 = 0; // sizeof(IntPtr);
int size5 = sizeof(decimal);
int size6 = sizeof(char);
int size7 = sizeof(bool);
int size8 = sizeof(byte);
int size9 = sizeof(Int16); // Equal to short
int size10 = sizeof(Int32); // Equal to int
int size11 = sizeof(Int64); // Equal to long
//
// Print each sizeof expression result.
//
Console.WriteLine("{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n{7}\n{8}\n{9}\n{10}",
size1, size2, size3, size4, size5, size6, size7, size8, size9, size10, size11);
}
}
Output
4
0
0
0
16
2
1
1
2
4
8
Description. The example program uses the sizeof operator repeatedly on different types and type aliases. The program has commented-out sizeof expressions, which would not compile because they attempt to compute the size of a reference type, which is never allowed.
Note: The .NET Framework uses a virtualized type layout system, which makes it impossible to consistently compute the memory usage of a reference type because the layout can change between versions. This program shows the size in bytes of several various value types.
Sizeof table. Here we note that each value type, including the double type that is not shown in the example, can have its sizeof expression computed at compile-time. If you are looking for a sizeof computation table, the MSDN reference is best—it is carefully checked for accuracy.
MSDN reference
Compile-time evaluation. Compiler theory breaks up the interpretation of computer programs into many phases. The sizeof operator can be evaluated during the initial compilation phase that occurs before execution. In other words, the sizeof expression can be evaluated statically, and therefore will cause no performance change from using a normal integer that equals the value. You can test this by inspecting the program in IL Disassembler, where you will see that no sizeof instructions exist in the intermediate language.
IL Disassembler Tutorial
Developers sometimes try to compute the sizeof result for a reference type at some point. If you do that, you will get the "Cannot take the address of" error shown below. Also, the sizeof operator in previous versions of the .NET Framework was allowable only in unsafe contexts, but this has been relaxed and you can now use sizeof anywhere on value types in your C# code.
Unsafe Keyword ValueTypeError 1
Cannot take the address of, get the size of, or declare a pointer to a managed type ('int[]')
Error 2
'int[]' does not have a predefined size, therefore sizeof can only be used in an unsafe context
(consider using System.Runtime.InteropServices.Marshal.SizeOf)

We examined the sizeof operator and sizeof expressions in the C# programming language. We saw the evaluation results of this operator on common value types and type aliases. Also, we noted the compilation system for the sizeof operator, which is that it is evaluated before the runtime occurs. Finally, we noted some errors that almost all developers will encounter when testing the sizeof operator.
Reflection