YLBTECH-MICROSOFT-CSHARPSAMPLES:YLBTECH-LANGUAGESAMPLES-STRUCT (structure) |
1.A, example (sample) back to top |
The Structure sample
This example demonstrates the syntax and use of structs. It also describes the significant differences between classes and structs. For more information, see objects, classes, and Structs (C # Programming Guide).
Safety Instructions |
This code example is provided to illustrate a concept that does not represent the most secure coding practice, so you should not use this code example in an application or Web site. Microsoft assumes no responsibility for incidental or consequential damages arising from the use of this code sample for other purposes. |
Build and run the structure sample in Visual Studio
In Solution Explorer, right-click the Struct1 project and select Set as Startup Project.
On the Debug menu, click Start Without Debugging.
Repeat the previous steps above for Struct2.
To build and run the structure sample from the command line
Use the "Change Directory" command to go to the "Struct1" directory.
Type the following command:
Use the "Change Directory" command to go to the "Struct2" directory.
Type the following command:
1.b,struct1 Sample Code back to top |
1.b.1, Struct1.cs
//Copyright (C) Microsoft Corporation. All rights reserved. //This code is published in compliance with//Microsoft Public License (MS-PL,http://opensource.org/licenses/ms-pl.html) of the terms. ////Copyright (C) Microsoft Corporation. All rights reserved. //Struct1.csusingSystem;structsimplestruct{Private intXval; Public intX {Get { returnXval; } Set { if(Value < -) Xval=value; } } Public voidDisplayx () {Console.WriteLine ("The stored value is: {0}", Xval); }}classtestclass{ Public Static voidMain () {simplestruct ss=Newsimplestruct (); Ss. X=5; Ss. Displayx (); }}
View Code
1.b.2,
1.b.exe,
is 5 Please press any key to continue ...
1.B
1.b,struct2 Sample Code 2 (sample codes) back to top |
1.b.1, Struct2.cs
//Copyright (C) Microsoft Corporation. All rights reserved. //This code is published in compliance with//Microsoft Public License (MS-PL,http://opensource.org/licenses/ms-pl.html) of the terms. ////Copyright (C) Microsoft Corporation. All rights reserved. //Struct2.csusingSystem;classtheclass{ Public intx;}structthestruct{ Public intx;}classtestclass{ Public Static voidStructtaker (thestruct s) {s.x=5; } Public Static voidClasstaker (Theclass c) {c.x=5; } Public Static voidMain () {thestruct a=Newthestruct (); Theclass b=NewTheclass (); A.x=1; b.x=1; Structtaker (a); Classtaker (b); Console.WriteLine ("a.x = {0}", a.x); Console.WriteLine ("b.x = {0}", b.x); }}
View Code
1.b.2,
1.b.exe,
15 Please press any key to continue ...
1.B,
1.C, (free Download) back to top |
|
Ylbtech Source: http://ylbtech.cnblogs.com/ This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility. |
YLBTECH-LANGUAGESAMPLES-STRUCT (structure)