Structure in C #

Source: Internet
Author: User

Similar to a class, the structure indicates the data structure that can contain data members and function members. However, unlike classes, the structure is a value type and does not require heap allocation. A variable of the structure type directly contains the data of the structure, and a variable of the class type contains references to the data.

Structure is particularly useful for small data structures with value semantics. Complex Numbers, vertices in the coordinate system, or "key-value" pairs in the dictionary are typical examples of structures. The key to these data structures is that they only have a small number of data members and do not require inheritance or reference identifiers, they are also suitable for convenient implementation using value semantics (copying a value directly when assigning a value instead of copying its reference ..

Structure Declaration
Structure declaration is a type declaration used to declare a new structure: Use the struct keyword

Access rhetoric struct structure name
{
Data Member
Member Functions
}

Differences between classes and structures
The structure is different from the class in the following important aspects:
• The structure is a value type, and the class is a reference type.
• In the structure, the instance field declaration cannot contain the variable Initial Value Setting item.
• The instance constructor without parameters cannot be declared in the structure.
• The Destructor cannot be declared in the structure.
• There is no inheritance in the structure. A structure cannot inherit from another structure or class, and cannot be the base class of a class. The structure can implement interfaces, and the implementation method is exactly the same as that of class implementation interfaces.

Example 1
This example declares a structure with five members: two attributes, one method, and two private fields. In this example, an instance of this structure is created and put into use:
Using system;
Struct point
{
Private int X, Y;
Public int x
{
Get
{
Return X;
}
Set
{
X = value;
}
}
Public int y
{
Get
{
Return y;
}
Set
{
Y = value;
}
}
Public void displaypoint ()
{
Console. writeline ("the point 'x value is: {0}", X );
Console. writeline ("the point 'y value is: {0}", y );
}
}

Class testclass
{
Public static void main ()
{
Point P = new point ();
P. x = 5;
P. Y = 5;
P. displaypoint ();
}
}
The new operator creates an instance with a structure, but the structure is a value type. It does not split the instance into memory space on the heap. The structure variable itself stores the value of the Instance rather than the instance reference. The new operator only calls the constructor of the structure.
Example 2
This example shows that when a structure is passed to a method, a copy of the structure is passed, and a reference is passed when a class instance is passed.
Using system;
Struct stpointx
{
Private int X;
Public int x
{
Get
{
Return X;
}
Set
{
X = value;
}
}
}

Class clpointx
{
Private int X;
Public int x
{
Get
{
Return X;
}
Set
{
X = value;
}
}
}

class testclass
{< br> Public static void structp (stpointx s)
{< br> S. X = 5;
}< br> Public static void classp (clpointx c)
{< br> C. X = 5;
}

Public static void main ()
{
Stpointx A = new stpointx ();
Clpointx B = new clpointx ();
A. x = 1;
B. x = 1;
Structp (a); // transmits a copy of the Instance
Classp (B); // transmits instance reference
Console. writeline ("A. X = {0}", A. X );
Console. writeline ("B. x = {0}", B. X );
}
}

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.