Sunwen Tutorial----C # Advanced (vii)

Source: Internet
Author: User
To say is the structure (struct) in C #, note that the structure I'm talking about here is not referring to the language structure of C #. Here is a kind of thing that is relative to the class, and I'm going to say this struct relative to the class.

The following example illustrates how to create a structure with properties, methods, and a field. And how to use him.

From://Structs\struct1.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 of this example is:

The stored value is:5
From the above example, we can see that the structure and the class seem to be the same. Indeed, if you use the class to re-write the program, the result is the same. But, obviously, two of the same things are impossible to come together. A struct is a value type, and a class is a reference type. So you can build objects like built-in types with structs.

Also, if you use a new keyword to create an instance of a class, it is allocated as a heap (heap), and when you use new to create an instance of a struct, it is allocated as a stack (stack). This will give us a lot more performance (M$ said). Okay, Let's take another look at the following example:


From://Structs\struct2.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 (a);
030:classtaker (b);
031:console.writeline ("a.x = {0}", a.x);
032:console.writeline ("b.x = {0}", b.x);
033:}
034:}

The output of this example is:

a.x = 1b.x = 5
As can be seen from this example, when a struct is passed to a method, it is passed only as a copy, and a reference is passed when a class is passed. So the a.x= output is 1, unchanged, and b.x has changed.

The difference is that the structure can be instantiated without new, and the class will. If you do not instantiate a struct with new, all fields will remain unassigned until all fields are initialized. Like classes, structs can execute interfaces. More importantly, the structure has no inheritance, A struct cannot inherit from another class, nor can it be a base class for another class.

Example three:

Interface IImage
{
void Paint ();
}

struct Picture:iimage
{
public void Paint ()
{
Painting Code goes here
}
private int x, y, Z; Other structs Members
}

The above is the Sunwen tutorial----C # Advanced (vii) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.