The structure in C #

Source: Internet
Author: User
Tags abstract object contains copy data structures interface string variable

Structs are defined using the struct keyword and are similar to classes, representing data structures that can contain data members and function members.
In general, we rarely use structs, and many people do not recommend the use of structs, but as a basic architecture in the. NET framework generic system, it is necessary to understand.
Characteristics of the structure:
A struct is a value type and does not require a heap allocation. A struct can be instantiated without using the new operator.
In a struct declaration, a field cannot be initialized unless it is declared as const or static. struct types are never abstract and are always implicitly sealed, so the abstract and sealed modifiers are not allowed in a struct declaration.
Structs cannot declare default constructors (constructors without parameters) or destructors, but can declare constructors with parameters. Structs can implement interfaces, but they cannot inherit from another struct or class, and cannot be the base of a class, all structures are directly inherited from System.ValueType, which inherits from System.Object. The structure is copied when it is assigned a value. When you assign a struct to a new variable, all data is copied, and any modifications made to the new copy do not change the original copy's data. Be sure to keep this in mind when working with collections of value types, such as dictionary<string, mystruct>. A variable of a struct type directly contains the data for that structure, whereas a variable of a class type contains only one reference to the corresponding data (the referenced data is called an "object"). However, structs can still be passed to function members through ref and out parameter references. A struct can be used as a nullable type, thus assigning null values to it. Structa
{publicintx;//cannot be directly assigned to it publicinty;
public static STRINGSTR = null; Static variables can initialize Publica (intx,inty)//with parameter constructors
{this.x =x;this.y =y;
Console.WriteLine ("X={0},y={1},str={2}", X, Y,STR);
}
}classprogram
{Staticvoidmain (string[] args)
{
A a =newa (1,2);
A A1 =a;
a.x = 10;
Console.WriteLine ("A1.x={0}", a1.x);
Console.read ();
}
}

The result is: x=1,y=2,str=
A1.x=1
At this point, the a1.x value is 1 because assigning a value to A1 is a copy of the value, so A1 will not be altered by a.x assignment.
But if a is a class, when x in a and A1 refers to the same address, the a1.x value is output 10.
Packing and unpacking of structures we know that a value of a class type can be converted to an object type or an interface type implemented by that class, which simply treats the corresponding reference as another type at compile time.
Similarly, a value of type object or an interface type can be converted back to a class type without having to change the corresponding reference. Of course, in this case, a run-time type check is required. Because structs are not reference types, the above actions are implemented in different ways for struct types.
When a value of a struct type is converted to type object or an interface type implemented by the struct, a boxing operation is performed.
Conversely, when a value of type object or the value of an interface type is converted back to a struct type, a unboxing operation is performed.
The main difference, compared to the same operations for class types, is:
The boxing operation copies the associated structure value into an instance that has been boxed, and the unboxing copies a structure value from the boxed instance.
As a result, changes to a structure outside of the box do not affect the boxed structure after the boxing or unboxing operation. Structprogram
{Staticvoidmain (string[] args)
{inti =1;objecto =i;//Implicit boxing

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.