C # (9) structure (Structs)

Source: Internet
Author: User

1.9 structure (Structs)

There are many similarities between the structure and the class. For example, the structure can implement the interface and have the same members as the class. There are also some important differences between structures and classes: the structure is a value type rather than a reference type, so inheritance is not supported! The structure is stored in the stack or is inline. The structure can improve the storage efficiency. For example, defining a structure with the same information as a class can greatly reduce the storage space. In the following example, the program creates and initializes 100 points. In the "Point" class, 101 independent objects need to be allocated ). */

Class Point
{
Public int x, y;
Public Point (){
X = 0;
Y = 0;
}
Public Point (int x, int y ){
This. x = x;
This. y = y;
}
}
Class Test
{
Static void Main (){
Point [] points = new Point [100];
For (int I = 0; I <100; I ++)
Points [I] = new Point (I, I * I );
}
}
/*

If "Point" is used as a structure, you can do this :*/

Struct Point
{
Public int x, y;
Public Point (int x, int y ){
This. x = x;
This. y = y;
}
}
/*

The Point is instantiated in the internal link, so it is optimized. Of course, mistakes are only counterproductive. For example, when we pass the structure, it will be slower than the transfer class. Because the structure is transmitted as a copy value, the class is the address of the reference value. The larger the data volume, the more obvious the gap.

Therefore, "There is no substitute for careful data structure and algorithm design." (I really don't want to translate pai_^ ).

1.10 interface (Interfaces)

Interface is used to define the contract of a program. With this contract, you can run the programming language restrictions (theoretically ). While the implementation Interface
The class or structure must be strictly consistent with the interface definition. The interface can contain the following members: method, attribute, index, and event. Example :*/

Interface IExample
{
String this [int index] {get; set ;}
Event EventHandler E;
Void F (int value );
String P {get; set ;}
}
Public delegate void EventHandler (object sender, Event e );
/*

The interface in the example contains an index, an event E, a method F, and a property P.
The interface supports multiple inheritance. In the following example, "IComboBox" is inherited from "ITextBox" and "IListBox" at the same time.

*/
Interface IControl
{
Void Paint ();
}
Interface ITextBox: IControl
{
Void SetText (string text );
}
Interface IListBox: IControl
{
Void SetItems (string [] items );
}
Interface IComboBox: ITextBox, IListBox {}
/*

The class and structure can be instantiated on multiple interfaces. In the following example, the class "EditBox" inherits the class "Control" and "IDataBound" and "IControl.
*/

Interface IDataBound
{
Void Bind (Binder B );
}
Public class EditBox: Control, IControl, IDataBound
{
Public void Paint ();
Public void Bind (Binder B ){...}
}
/*

In the above Code, the "Paint" method comes from the "IControl" interface; the "Bind" method comes from the "IDataBound" interface, all are implemented in the EditBox class as "public.

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.