Example analysis of sizeof usage in C #

Source: Internet
Author: User

This article mainly describes the use of sizeof in C #, including the common usage and annotation matters, the needs of friends can refer to.

sizeof is a very important method in C #, and this article analyzes the usage of sizeof in C # as an example. Share to everyone for your reference. The specific analysis is as follows:

In C #, sizeof is used to calculate the size of a type, in bytes. There is a class like this:

123456 publicclass MyUglyClass { public char myChar1; public int myInt; public charmyChar2; }

On the client, an attempt was made to calculate the size of the type using sizeof.

1234567891011 class Program { static void Main(string[] args) { MyUglyClass m = new MyUglyClass(); m.myChar1 = ‘d‘; m.myInt = 25; m.myChar2 = ‘a‘; Console.WriteLine(sizeof(MyUglyClass)); } }

The error after running is as follows:

0 first error stating that to use sizeof, you must use the keyword unsafe
0 The second error indicates that sizeof is invalid for run-time variables and can only be sized for compiler variables

Change the class to a struct value type.

123456 publicstruct MyUglyClass { public char myChar1; public int myInt; public charmyChar2; }

The client is changed to the following:

123456789101112131415 class Program { static void Main(string[] args) { MyUglyClass m = new MyUglyClass(); m.myChar1 = ‘d‘; m.myInt = 25; m.myChar2 = ‘a‘; unsafe { Console.WriteLine(sizeof(MyUglyClass)); } } }

Run, continue to error: "Unsafe code will only appear when using/unsafe compilation."
The workaround is: right-click project → properties → generate → tick "allow unsafe code" → Save

Run again, results: 12

Again, in Myuglyclass this value-type structure, the char type is 16 bits, which is equivalent to 2 bytes, an int type of 32 bits, and a 4-byte equivalent. Myuglyclass type size =2+2+4=8 bytes, should be 8 bytes! How can it be 12 bytes?

This involves the alignment and padding of the stack. Take the above example: Originally, the stack has an int type of variable accounted for 4 bytes, 2 char types of variables accounted for 2 bytes, when these variables on the stack are arranged, the stack is aligned, that is, all the smaller bytes of the variable to the largest byte of the variable, and fill the empty space.

The Red Fork part is filled in order to align.

You can use the [StructLayout] attribute if you want to ignore the part that fills in order to align.

1234567 [StructLayout(LayoutKind.Auto)] publicstruct MyUglyClass { public char myChar1; public int myInt; public charmyChar2; }

Run again, results: 8

Summary:sizeof applies only to value types and needs to be used in the unsafe context .

It is believed that this article has some reference value for the study of C # program design.

Example analysis of sizeof usage in C #

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.