Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of the Constructor function of C # structure

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you an example analysis of the constructor of the C# structure. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

What is the application of the C # structure constructor?

An example of the constructor of C # structure body 1.

/ / < summary > / / start the parameter structure of the server / / < / summary > public struct SERVER_VIDEOINFO {/ / < summary > / Channel data type, whether telephone line connection / / < / summary > byte [] m_datatype is supported. / / < summary > / / the number of channels in the system / / < / summary > byte massichanum; / / < summary > / timeout wait time, 1-300, in minutes / / used to set the timeout wait time, / / if the m_waittime does not receive an answer signal from a user, cut off the connection with that user. / / < / summary > uint massively waittime; / / < summary > / / currently not used / / < / summary > uint massively bufnumb; public SERVER_VIDEOINFO (byte channum, uint waittime, uint bufnum) {this.m_channum = channum; this.m_waittime = waittime; this.m_bufnum = bufnum; this.m_datatype = new byte [64];}}

Example of C # structure constructor 2.

/ / < summary > / / start the parameter structure of the server / / < / summary > public unsafe struct SERVER_VIDEOINFO {/ / < summary > / Channel data type, whether telephone line connection is supported / / < / summary > fixed byte m_datatype [64] / / < summary > / / the number of channels in the system / / < / summary > byte massichanum; / / < summary > / timeout waiting time, 1-300, in minutes / / used to set the timeout waiting time, / if the m_waittime does not receive an answer signal from a user, cut off the connection with that user. / / < / summary > uint massively waittime; / / < summary > / / currently does not use / / < / summary > uint massively bufnum.}; / * Note: all structures in C# have a default constructor without parameters, which is used to initialize the fields of the structure, and the default constructor of the structure cannot be overridden. The array defined in the structure can also be initialized by passing parameters in the constructor! 1 and 2 have the same effect, but project properties / generate / allow unsafe code needs to be selected if defined in a 2 way. , /

The concept of the C # constructor comes from the MSDN material:

For C # structures, structures and classes share most of the same syntax, but structures are more restricted than classes:

In a structure declaration, a field cannot be initialized unless it is declared as const or static.

Structures cannot declare default constructors (constructors with no arguments) or destructors.

Because copies of the structure are automatically created and destroyed by the compiler, default constructors and destructors are not required. In fact, the compiler implements the default constructor by assigning default values to all fields (see the default values table (C# reference). Structures cannot inherit from classes or other structures.

Structure is a value type. If you create an object from a structure and assign it to a variable, the variable contains all the values of the structure. When you copy a variable that contains a structure, all data is copied, and any changes made to the new copy do not change the data of the old copy. Because the structure does not use references, the structure is not identified; two instances of value types with the same data cannot be distinguished. All value types in C # inherit from ValueType, which inherits from Object.

The application of C # structure constructor has the following characteristics:

Structure is a value type, while a class is a reference type.

Unlike classes, structures can be instantiated without using the new operator.

Structures can declare constructors, but they must take parameters.

A structure cannot inherit from another structure or class and cannot be the base of a class. All structures inherit directly from System.ValueType, which inherits from System.Object.

Structure can implement the interface.

Structure can be used as a type that can be null, so you can assign a null value to it.

The above is the example analysis of the C# structure constructor shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report