YLBTECH-LANGUAGESAMPLES-STRUCT (structure)

Source: Internet
Author: User

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
    1. In Solution Explorer, right-click the Struct1 project and select Set as Startup Project.

    2. On the Debug menu, click Start Without Debugging.

    3. Repeat the previous steps above for Struct2.

To build and run the structure sample from the command line
    1. Use the "Change Directory" command to go to the "Struct1" directory.

    2. Type the following command:

      CSC Struct1.csstruct1
    3. Use the "Change Directory" command to go to the "Struct2" directory.

    4. Type the following command:

      CSC struct2.csstruct2
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)

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.