C # 2.0 Learning --Yield

xiaoxiao2021-04-08  291

// Copyright (c) Microsoft Corporation. All Rights Reserved.

Using system.collections.generic; use system.text;

Namespace yield {class yield {public static class number of integers. public static int □ IntS = {1, 2, 3, 5, 5, 8, 13, 21, 34, 55, 89, 144, 233 , 377};

// define a property That Returns Only the evenumerable getEven () {// use yield to return the even number in the list. Foreach (INT i in INTS) IF (I% 2 == 0 ) Yield return i;}

// define a property That Returns Only the even number getodd () {// use yield to return only the odd number. Foreach (INT i in INTS) IF (I% 2 == 1) Yield Return i;}}

Static void main (string [] args) {

// Display the even number. Console.writeline ("Even Numbers"); Foreach (INT I in Numberlist.Geteven ()) Console.WriteLine (i);

// Display the odd number. Console.writeline ("odd number"); foreach (int I in numberlist.getodd ()) Console.Writeline (i);}}}

CSC Yield.cs

Yield

转载请注明原文地址:https://www.9cbs.com/read-132910.html

New Post(0)