Theme C # the sizeof operator in the tutorial [original]

Source: Internet
Author: User

The size of the byte used to obtain the value type. For example, the int size can be retrieved as follows:

 

Int intSize = sizeof (int );

 

Console. WriteLine (intSize );

 

The sizeof operator applies only to the value type, not the reference type.

 

The sizeof operator cannot be overloaded. The value returned by the sizeof operator is of the int type. The following table shows the constant values of some predefined types.

 

Expression Result
Sizeof (sbyte) 1
Sizeof (byte) 1
Sizeof (short) 2
Sizeof (ushort) 2
Sizeof (int) 4
Sizeof (uint) 4
Sizeof (long) 8
Sizeof (ulong) 8
Sizeof (char) 2 (Unicode)
Sizeof (float) 4
Sizeof (double) 8
Sizeof (bool) 1

 

For all other types (including struct), the sizeof operator can only be used in insecure code blocks. Although the SizeOf method can be used, the values returned by this method are not always the same as those returned by sizeof. Inclual. SizeOf returns the size after the encapsulated processing type, while sizeof returns the size allocated by the Common Language Runtime (including any blank space ).

 

This article is a new one. For more information, see the source and author!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.