Tutorial Sunwen Tutorial----C # Advanced
Seven
Mrfat@china.com
Hello everybody, I am Wuhan Hua Division's Sunwen. It is May 4 noon, yesterday day because in doing their own technology website, called "Devil Mountain Technology Station", (hey, a bit scary!) So there's no writing. When this station is built, we will have to join the crowd, we are mainly facing the education network, because the server is in the Education network.
Okay, here we are. What I'm talking about is the structure in C # (struct), and notice that the structure I'm talking about here is not the language structure of C #. Here is a kind of relative to the class (class), I compare with the class, to say this struct.
The following example illustrates how to build a structure that has attributes, methods, and a field. and tells how to use him.
M://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 example above we can see that the structure and class seem to be the same. Indeed, if you use classes to repeat the program, the result is the same. But, obviously, two of the same things are unlikely to happen together. The struct (struct) is a value type, and the class is a reference type. In this way, you can build objects like built-in types with structs.
And if you use a new keyword to create an instance of a class, it is allocated as a heap (heap), and when new is used to create an instance of a struct, it is allocated in stacks (stack). This will give us a lot of performance (M$ said). All right, Let's take another look at the following example:
a.x = 1b.x = 5
From this example, it can be seen that when a struct is passed to a method, it is simply a copy, and a class is passed as a reference. So the a.x= output is 1, unchanged, and b.x is changed.
The other difference is that structs can be instantiated without new and the class is going to. If you don't use new to instantiate a struct, all the fields will still be unassigned until all the fields are initialized. As with classes, structs can perform interfaces. More importantly, there is no inheritance of structure, 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 struct members
}
Well, I'll talk about the structure, and I'll talk about it later.
Next page
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