Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio .NET
Reference
Visual C# Language
C# Keywords
Operator Keywords
 sizeof

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2003/.NET Framework 1.1

Other versions are also available for the following:
C# Programmer's Reference
sizeof

The sizeof operator is used to obtain the size in bytes for a value type. A sizeof expression takes the form:

sizeof(type)

where:

type
The value type for which the size is obtained.

Remarks

The sizeof operator can be applied only to value types, not reference types.

The sizeof operator can only be used in the unsafe mode.

The sizeof operator cannot be overloaded.

Example

// cs_operator_sizeof.cs
// compile with: /unsafe

// Using the sizeof operator
using System;
class SizeClass 
{
   // Notice the unsafe declaration of the method:
   unsafe public static void SizesOf() 
   {
      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));
   }
}

class MainClass 
{
   public static void Main() 
   {
      SizeClass.SizesOf();
   }
}

Output

The size of short is 2.
The size of int is 4.
The size of long is 8.

See Also

C# Keywords | Operator Keywords

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker