Hello, everyone, I am SUNWEN from Wuhan huashi. It's noon on October 11, May 4. One day yesterday, I was engaged in my own technical website called "ghost Hill technology station". (Hey, it's a little scary !) So I didn't write it. After this station is built, I 'd like to hear from you more. Our station is mainly for CERNET, because the server is in CERNET.
Now, let's get down to the truth. I want to talk about the structure (struct) in C #. Note that the structure I mentioned here is not the language structure of C. 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.