欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

C#中yield用法使用說明

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次

在迭代器塊中用于向枚舉數(shù)對象提供值或發(fā)出迭代結(jié)束信號。它的形式為下列之一:
yield return <expression>;
yield break;
備注:
計算表達式并以枚舉數(shù)對象值的形式返回;expression 必須可以隱式轉(zhuǎn)換為迭代器的 yield 類型。
yield 語句只能出現(xiàn)在 iterator 塊中,該塊可用作方法、運算符或訪問器的體。這類方法、運算符或訪問器的體受以下約束的控制:不允許不安全塊。
方法、運算符或訪問器的參數(shù)不能是 ref 或 out。
yield 語句不能出現(xiàn)在匿名方法中。
當(dāng)和 expression 一起使用時,yield return 語句不能出現(xiàn)在 catch 塊中或含有一個或多個 catch 子句的 try 塊中。
yield return 提供了迭代器一個比較重要的功能,即取到一個數(shù)據(jù)后馬上返回該數(shù)據(jù),不需要全部數(shù)據(jù)裝入數(shù)列完畢,這樣有效提高了遍歷效率。

以下是一個比較特殊的例子:

C# 中yield 的用法代碼引用:

using System;
using System.Collections;
using System.IO;
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
using System.Windows.Forms;
using System.Threading;

namespace test
{
  public class Persons : System.Collections.IEnumerable
  {
    #region IEnumerable 成員

    public System.Collections.IEnumerator GetEnumerator()
    {
      yield return "1";
      Thread.Sleep(5000);
      yield return "2";
      Thread.Sleep(5000);
      yield return "3";
      Thread.Sleep(5000);
      yield return "4";
      Thread.Sleep(5000);
      yield return "5";
      Thread.Sleep(5000);
      yield return "6";
    }

    #endregion
  }

  class program
  {
    static void Main()
    {
      Persons arrPersons = new Persons();
      foreach (string s in arrPersons)
      {
        System.Console.WriteLine(s);
      }

      System.Console.ReadLine();
    }
  }  
}

每隔5秒鐘, 控制臺就會輸出一個數(shù)據(jù),直到全部數(shù)據(jù)輸入完畢。

以上就是關(guān)于C#中yield用法使用說明,希望對大家的學(xué)習(xí)有所幫助。

上一篇:C#編程和Visual Studio使用技巧(下)

欄    目:C#教程

下一篇:積累Visual Studio 常用快捷鍵的動畫演示

本文標(biāo)題:C#中yield用法使用說明

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6905.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有