C # advanced tutorial (7) Structure in C # (struct)

Source: Internet
Author: User

Structure (struct) in C)

I want to talk about C #. Note that the structure I mentioned here does not refer to the C # language structure. here we talk about something relative to a class. Next I will compare it with a class To Talk About This struct.

The following example describes how to create a structure with attributes, methods, and fields and how to use them.

000: // Structsstruct1.cs
001: using System;
002: struct SimpleStruct
003 :{
004: private int xval;
005: public int X
006 :{
007: get {
008: return xval;
009 :}
010: set {
011: if (value <100)
012: xval = value;
013 :}
014 :}
015: public void DisplayX ()
016 :{
017: Console. WriteLine ("The stored value is: {0}", xval );
018 :}
019 :}
020:
021: class TestClass
022 :{
023: public static void Main ()
024 :{
025: SimpleStruct ss = new SimpleStruct ();
026: ss. X = 5;
027: ss. DisplayX ();
028 :}
029 :}

The output in this example is:

The stored value is: 5
From the above example, we can see that the structure and class seem to be the same. indeed, if you use the class to re-write this program, the results will be the same. however, it is obvious that two identical things cannot appear together. the structure (struct) is of the value type, and the class is of the reference type. in this way, you can use the structure to create objects of the built-in type.

In addition, if you use the new keyword to create a class instance, it is allocated by heap and the new is used to create a structured instance, it is allocated by stack. this will give us a lot of Performance Improvement (M $ said ). let's take a look at the following example:

000: // Structsstruct2.cs
001: using System;
002:
003: class TheClass
004 :{
005: public int x;
006 :}
007:
008: struct TheStruct
009 :{
010: public int x;
011 :}
012:
013: class TestClass
014 :{
015: public static void structtaker (TheStruct s)
016 :{
017: s. x = 5;
018 :}
019: public static void classtaker (TheClass c)
020 :{
021: c. x = 5;
022 :}
023: public static void Main ()
024 :{
025: TheStruct a = new TheStruct ();
026: TheClass B = new TheClass ();
027: a. x = 1;
028: B. x = 1;
029: structtaker ();
030: classtaker (B );
031: Console. WriteLine ("a. x = {0}", a. x );
032: Console. WriteLine ("B. x = {0}", B. x );
033 :}
034 :}

The output in this example is:

A. x = 1b. x = 5
From this example, we can see that when a structure is passed to a method, it is only a copy, and when a class is passed, it is a reference. so. x = the output is 1, unchanged, and B. x has changed.

Another difference is that the structure can be instantiated without new, but the class needs. if you do not need new to instantiate a structure, all fields will remain unallocated until all fields are initialized. like a class, the structure can execute interfaces. more importantly, the structure has no inheritance. A structure cannot be inherited from other classes or be the base class of other classes.

Example 3:

Interface IImage
{
Void Paint ();
}

Struct Picture: IImage
{
Public void Paint ()
{
// Painting code goes here
}
Private int x, y, z; // other struct members
}
Now, I will talk about the structure and will talk about it later.

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.