C# 2.0学习之--Yield
2006-06-23 18:52
447 查看
//Copyright (C) Microsoft Corporation. All rights reserved.
using System;
using System.Collections.Generic;
using System.Text;
namespace Yield
{
class Yield
{
public static class NumberList
{
// Create an array of integers.
public static int[] ints = { 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377 };
// Define a property that returns only the even numbers.
public static IEnumerable<int> GetEven()
{
// Use yield to return the even numbers in the list.
foreach (int i in ints)
if (i % 2 == 0)
yield return i;
}
// Define a property that returns only the even numbers.
public static IEnumerable<int> GetOdd()
{
// Use yield to return only the odd numbers.
foreach (int i in ints)
if (i % 2 == 1)
yield return i;
}
}
static void Main(string[] args)
{
// Display the even numbers.
Console.WriteLine("Even numbers");
foreach (int i in NumberList.GetEven())
Console.WriteLine(i);
// Display the odd numbers.
Console.WriteLine("Odd numbers");
foreach (int i in NumberList.GetOdd())
Console.WriteLine(i);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Yield
{
class Yield
{
public static class NumberList
{
// Create an array of integers.
public static int[] ints = { 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377 };
// Define a property that returns only the even numbers.
public static IEnumerable<int> GetEven()
{
// Use yield to return the even numbers in the list.
foreach (int i in ints)
if (i % 2 == 0)
yield return i;
}
// Define a property that returns only the even numbers.
public static IEnumerable<int> GetOdd()
{
// Use yield to return only the odd numbers.
foreach (int i in ints)
if (i % 2 == 1)
yield return i;
}
}
static void Main(string[] args)
{
// Display the even numbers.
Console.WriteLine("Even numbers");
foreach (int i in NumberList.GetEven())
Console.WriteLine(i);
// Display the odd numbers.
Console.WriteLine("Odd numbers");
foreach (int i in NumberList.GetOdd())
Console.WriteLine(i);
}
}
}
csc Yield.cs Yield
相关文章推荐
- C# 2.0学习之--Yield
- C#2.0泛型学习--入门篇
- C#学习笔记---C#2.0范型
- C#学习笔记三: C#2.0泛型 可控类型 匿名方法和迭代器
- C# Kinect 2.0 SDK 学习笔记(0)——基本情况介绍
- C#2.0 IO学习之读写二进制文件
- [CSharp]C#2.0 锐利体验系列课程 (1):泛型编程-学习笔记
- C# 2.0学习之--匿名代理
- C# 2.0学习之--代理1
- C# 2.0学习之--事件2
- [读书笔记]C#学习笔记四: C#2.0泛型 可控类型 匿名方法和迭代器
- C# .Net 2.0实例学习:WebBrowser页面与WinForm交互技巧(二)
- C#2.0泛型学习
- 系统的学习C#2.0中的泛型
- C#2.0 新的关键字 yield
- 系统的学习C#2.0中的泛型
- 【C#学习】yield关键字
- C#2.0泛型学习--入门篇
- C# 2.0学习之--数组
- C# 2.0学习之--条件编译