C#?????yield??Singleton
???????????? ???????[ 2016/7/6 10:25:23 ] ??????????????????? C#
????1.???????yiled??????
???????μ?java?????????μ???yield??????????????????????????????????C#?й???yield?????????yield??????????????????????У???????????2???????“????”?????????????????????????????????????????????????????yield return??yield break????????2??С??????????????yield???????yield????????????????????????????е?GetResult()????????????????????????????????????????????????????????????????????0????????????????????λ???????????0??????????????????????????????е???MoveNext()????????????????????????????????????????????????????檔????????????????????????GetResultByYield()??????????????????????????????????????????????н???GetResultByYield()????????????????result.Current??null??????????????????з???????????????????2???????????????????????????MoveNext()???????????GetResultByYield()????????е?yield return?漴????main()???????????????????????е????
??????????????????е???MoveNext()?????????????з???????y???????????????????κν??????????????????????????????????????????????????for????????????yield return????????????С??????????С?????????????????????????????????????????????????????????????????????????С????????????????????????μ??????????????????????????????????????????????????????????????????????С?0.2????С???????????????????????????????0.2?????????????????????????????????????????????????????????????????????????????????????????????????????yield return??????while??????????дyield break?????????????????????
/// <summary>
/// ?????yield?????
/// </summary>
class Program
{
static void Main(string[] args)
{
//?????????????
var result = GetResult();
//?????????
Console.WriteLine(result.Current);
Console.WriteLine("???????");
while (result.MoveNext())
{
Console.WriteLine(result.Current);
}
Console.WriteLine("????????");
Console.ReadLine();
}
//?????yield?????е???
static IEnumerator<int> GetResult()
{
var arr = new int[] { 1?? 6?? 8?? 12??15};
List<int> list = new List<int>();
foreach (int item in arr)
{
if (item < 12)
list.Add(item);
}
return list.GetEnumerator();
}
}
/// <summary>
/// ???yield?????
/// </summary>
class Program
{
static void Main(string[] args)
{
//?????????????
var result = GetResultByYield();
//?????????
Console.WriteLine(result.Current);
Console.WriteLine("???????");
while (result.MoveNext())
{
Console.WriteLine(result.Current);
}
Console.WriteLine("????????");
Console.ReadLine();
}
//???yield?????е???
static IEnumerator GetResultByYield()
{
var arr = new int[] { 1??6??8??12??15};
foreach (var item in arr)
{
yield return item;
if (item == 12)
yield break;
}
}
}
?????????????£?
????2.????yield
?????????????????????Reflector?????У??????????????δ????????????????Pragrom???C#???????????<GetResultByYield>d__0??????C#???????????????GetResultByYield()??????IL?????????δ????п????????????????????<GetResultByYield>d__0??????????????????Щ???????????Σ?????????????????????????????????????MoveNext()??????Current?????????δ????????????????C#????????????????????????к??????????????????????????????????????????????δ????У??????????GetResultByYield()?????в???б??????飬????????п????????????newarr???????newobj??????<GetResultByYield>d__0?????????????????????????????????????????GetResultByYield()?????????????????????????????????????MoveNext()?????С???滹??yield???????????????????????????????yield return??????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11