Ylbtech-languagesamples-yield

Source: Internet
Author: User

Ylbtech-microsoft-csharpsamples:ylbtech-languagesamples-yield

1.A, example (sample) back to top
"Yield" Example

This example shows how to create a list class to implement the IEnumerable and yield keywords to enable foreach iterations for the contents of the list. This example defines two properties, one of which returns an odd number, and the other returns an even number.

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.

To build and run the Yield code sample in Visual Studio
    • On the Debug menu, click Start Without Debugging.

To build and run the Yield code sample from the command line
    1. Use the Change directory (CD) command to go to the Yield directory.

    2. Type the following command:

      CSC Yield.csyield
1.B, sample code back to top

1.b.1, Yield.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. usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceyield{classYield { Public Static classNumberList {//creates an array of integers.              Public Static int[] INTs = {1,2,3,5,8, -, +, the, -, the,144,233,377 }; //defines a property that returns only even numbers.              Public Staticienumerable<int>Geteven () {//use yield to return an even number in the list.                 foreach(intIinchints)ifI2==0)                        yield returni; }            //defines a property that returns only even numbers.              Public Staticienumerable<int>getodd () {//use yield returns only odd numbers.                 foreach(intIinchints)ifI2==1)                        yield returni; }        }        Static voidMain (string[] args) {            //Show even numbers. Console.WriteLine ("even numbers"); foreach(intIinchNumberlist.geteven ())            Console.WriteLine (i); //show odd numbers. Console.WriteLine ("ODD Numbers"); foreach(intIinchnumberlist.getodd ())        Console.WriteLine (i); }    }}
View Code

1.b.2,

1.b.exe,

even numbers 2 8  the 144 ODD Numbers 1 3 5  -  +  -  the 233 377 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-yield

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.