Discuss the union structure in. NET

Source: Internet
Author: User

Introduction: when programming some algorithms in C language, you need to store several different types of variables in the same memory unit. That is to say, using the overwrite technology, several variables overwrite each other. These variables occupy a memory structure. In C, these variables are called the "shared body" type structure.

In C ++, a type is called a consortium (also called a shared body). Its keyword is union, which is very similar to the structure struct and can contain any structure data, however, it has another unique feature, that is, all data points to an address.

This means that all data in a consortium references data with the same address in a memory block. When we change the value of any data in the consortium, the values of other data will change accordingly.

This is very effective for data of unknown types. You can use a consortium to load a data, analyze whether the data is valid, or perform bitwise operations on some special types, obtains the value at a specific position.

However, in VB. NET or C #, there is no union keyword that makes us famous for the Consortium. But how can we be famous for the consortium?

This requires structure attributes!

Let's take a look at how to convert the following C ++ consortium code into a VB. NET consortium structure!

  1. UnionMyunion
  2. {
  3. CharB; // a single-byte integer. In C, the char type is used to represent a single-byte integer.
  4. ShortS; // double-byte integer
  5. IntI; // four-byte integer
  6. }

The size of this consortium is 4 bytes, each of which is represented as a single-byte, dual-byte, and four-byte integer. Any data changes during running will affect other data.

  1. Improts System. Runtime. interopservices' introduce the Runtime unmanaged Data Management Service

Introduce structure attributes to precisely control the positions of elements in the Structure

  1. <StructLayout (LayoutKind. Explicit)> _
  2. StructureMyUnion
  3. 'Set the Offset Value of the field to 0.
  4. <FieldOffset (0)>DimBAs Byte'Single-byte integer
  5. <FieldOffset (0)>DimSAs Short'Dubyte integer
  6. <FieldOffset (0)>DimIAs Integer'Four-byte integer
  7. End Structure

This is the method for setting the consortium structure in. NET. The setting method in C # In VB. NET is the same. I will not repeat it here.

The following describes the feature application of a consortium. The code below demonstrates the features of a consortium based on the well-known consortium structure as an example:

  1. DimMUAs NewMyUnion
  2. MsgBox (String. Format ("{0} {1} {2}", MU. B, MU. s, MU. I) '0 0 0
  3. MU. s = Int16.MaxValue
  4. MsgBox (String. Format ("{0} {1} {2}", MU. B, MU. s, MU. I) '2017 255 32767
  5. MU. B = 12
  6. MsgBox (String. Format ("{0} {1} {2}", MU. B, MU. s, MU. I) '12 32524 32524
  7. MU. I = 0
  8. MsgBox (String. Format ("{0} {1} {2}", MU. B, MU. s, MU. I) '0 0 0

The code above can display the changes in the data in the memory more intuitively. When the Union data is changed, other data also changes with the memory.

Of course, there are limits on the use of consortium, that is. NET consortium is only applicable to value types and cannot be applied to reference types and pointers. You cannot set strings or arrays to enter the consortium type. This requires great attention!

Of course, the Consortium struct in. NEt is not only used above. You can use your imagination, for example:

  1. <StructLayout (LayoutKind. Explicit)> _
  2. StructureMyUnion2
  3. <FieldOffset (0)>DimB1As Byte 
  4. <FieldOffset (1)>DimB2As Byte 
  5. <FieldOffset (2)>DimB3As Byte 
  6. <FieldOffset (3)>DimB4As Byte 
  7. <FieldOffset (0)>DimIAs Integer 
  8. <FieldOffset (0)>DimUiAsUInteger
  9. End Structure

This structure can be used to obtain the data of each byte of a four-byte integer with or without the need to write algorithms for analysis.

Test code:

Code:

  1. DimMUAsMyUnion2
  2. MsgBox (MU. I & ":" & MU. ui) '0: 0
  3. MsgBox (String. Format ("{0} {1} {2} {3}", Hex (MU. b1), Hex (MU. b2), Hex (MU. b3), Hex (MU. b4) '0 0 0 0
  4. MU. b1 = 255: MU. b2 = 255: MU. b3 = 255: MU. b4 = 255
  5. MsgBox (String. Format ("{0} {1} {2} {3}", Hex (MU. b1), Hex (MU. b2), Hex (MU. b3), Hex (MU. b4) 'ff FF
  6. MsgBox (MU. I & ":" & MU. ui) '-1: 4294967295

 

Related Article

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.