Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

sizeof: practical reference

sizeof: practical reference: sizeof returns a type's size in bytes; in safe context it is permitted only for a built-in unmanaged type with platform-independent size or an enum.

Version: C# operator reference; verified against C# 14

Syntax

sizeof(type)

sizeof returns a type's size in bytes; in safe context it is permitted only for a built-in unmanaged type with platform-independent size or an enum.

Use it in interop and binary code with an explicit layout.

The size of a managed type is not the object's total heap consumption.

Another unmanaged type, including a custom struct, may require unsafe; pointer size depends on architecture.

The complete program actually uses sizeof; its exact expected stdout is 8.

Examples

using System;
using System.Linq;
using System.Threading.Tasks;

Console.WriteLine(sizeof(long));

Result

8

The complete program actually uses `sizeof`; its exact expected stdout is `8`.

Sources