Home
Search
sizeof KeywordUse the sizeof operator. Sizeof returns the number of bytes in a type.
C#
This page was last reviewed on Jun 28, 2021.
Sizeof. This C# operator returns a size: this is the number of bytes a type (the argument) uses. Due to the virtualized type layout, sizeof is limited.
Sizeof limits. Sizeof can only compute the byte size of value types. Because of its limitations, the sizeof operator is often incorrectly used.
Example. This program uses many local variables and assigns these locals to the result of sizeof expressions. Some expressions are commented out—these won't compile.
Compile-time Sizeof is evaluated at compile-time. When you execute this program, it will just use constant numbers.
Important The program does not compute the size of reference types such as string or array—this is not possible.
Info We see commented-out sizeof expressions, which would not compile because they attempt to compute the sizeof a reference type.
Note In .NET, memory layout can change between versions—so computing it is harder.
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); } }
4 0 0 0 16 2 1 1 2 4 8
Errors. Developers sometimes try to compute the size of result for a reference type at some point. If you do that, you will get the "Cannot take the address of" error shown.
Important Sizeof does not support reference types. We can only use it on things like int and uint.
Also Size in previous versions of .NET was allowable only in unsafe contexts, but this has been relaxed. You can now use sizeof anywhere.
Unsafe
ValueType
Error 1:
Cannot take the address of, get the size of, or declare a pointer to a managed type (int[])
int[] does not have a predefined size, therefore sizeof can only be used in an unsafe context (consider using System.Runtime.InteropServices.Marshal.SizeOf)
Performance notes. Compiler theory breaks up the interpretation of computer programs into many phases. The sizeof operator can be evaluated during the initial compilation phase.
And This occurs before execution. You will see that no sizeof instructions exist in the intermediate language.
A summary. We examined the sizeof operator and sizeof expressions. And we saw the evaluation results of this operator on common value types and type aliases.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 28, 2021 (simplify).
Home
Changes
© 2007-2023 Sam Allen.