C#为什么这些使用委托和泛型的方法中的一些没有启动?
我试图学习一些泛型和SelectMany函数和委托。下面代码的语义功能(比如函数应该做什么)并不重要。其中大多数都没有做任何有意义的事情,只是试图看看函数没有启动的问题是否仍然存在 这是我的问题。。为什么名为C#为什么这些使用委托和泛型的方法中的一些没有启动?,c#,.net,generics,methods,delegates,C#,.net,Generics,Methods,Delegates,我试图学习一些泛型和SelectMany函数和委托。下面代码的语义功能(比如函数应该做什么)并不重要。其中大多数都没有做任何有意义的事情,只是试图看看函数没有启动的问题是否仍然存在 这是我的问题。。为什么名为\u SelectManyIteratora的方法没有启动。也不选择manyiteratorb。但是\u选择manyiteratorc确实会启动 就像在中一样,我正在调用函数,没有得到任何编译错误,函数的第一行,一个简单的WriteLine语句,没有执行 我本以为他们都会开始 我的兴趣是 u
\u SelectManyIteratora
的方法没有启动。也不选择manyiteratorb
。但是\u选择manyiteratorc
确实会启动
就像在中一样,我正在调用函数,没有得到任何编译错误,函数的第一行,一个简单的WriteLine语句,没有执行
我本以为他们都会开始
我的兴趣是
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace functionwontstart
{
class Program
{
static void Main(string[] args)
{
string[] arrstr1 = { "abc", "def", "ghi" };
string[] arrstr2= {"qrs","tuv","wxy"};
_SelectManyIteratora(arrstr1, x => arrstr2, (s1, s2) => s1 + s2);
_SelectManyIteratorb(arrstr1, x => arrstr2, (s1, s2) => s1 + s2);
_SelectManyIteratorc(arrstr1, x => arrstr2);
Console.ReadLine();
}
static IEnumerable<TResult> _SelectManyIteratora<TSource, TCollection, TResult>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)
{
Console.Write("got this far"); // no!
foreach (TSource element in source)
foreach (TCollection subElement in collectionSelector(element))
yield return resultSelector(element, subElement);
}
static IEnumerable<TResult> _SelectManyIteratorb<TSource, TCollection, TResult>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)
{
Console.Write("got this far"); // no!
TSource ts = source.ElementAt(0);
TCollection tc = collectionSelector(ts).ElementAt(0);
yield return resultSelector(ts, tc);
}
static IEnumerable<int> _SelectManyIteratorc<TSource, TCollection>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TCollection>> collectionSelector)
{
Console.Write("got this far"); // yes
return new List<int>();
}
} //class
} //namespace
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用系统集合;
名称空间函数wontstart
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串[]arrstr1={“abc”、“def”、“ghi”};
字符串[]arrstr2={“qrs”、“tuv”、“wxy”};
_选择多种畸形(arrstr1,x=>arrstr2,(s1,s2)=>s1+s2);
_选择多个畸胎体(arrstr1,x=>arrstr2,(s1,s2)=>s1+s2);
_选择manyiteratorc(arrstr1,x=>arrstr2);
Console.ReadLine();
}
静态IEnumerable\u SelectManyIteratora(IEnumerable源、Func collectionSelector、Func resultSelector)
{
Console.Write(“走了这么远”);//不!
foreach(源中的TSource元素)
foreach(collectionSelector(element)中的收集子元素)
收益返回结果选择器(元素、子元素);
}
静态IEnumerable\u SelectManyIteratorb(IEnumerable源、Func collectionSelector、Func resultSelector)
{
Console.Write(“走了这么远”);//不!
TSource ts=source.ElementAt(0);
t收集tc=收集选择器(ts).ElementAt(0);
收益率返回结果选择器(ts、tc);
}
静态IEnumerable\u selectManyIterator(IEnumerable源、Func collectionSelector)
{
Console.Write(“走了这么远”);//是的
返回新列表();
}
}//类
}//名称空间
这些方法是迭代器-它们使用产生返回。他们以一种特殊的方式执行。。。代码仅在迭代结果时执行
因此,如果将调用代码更改为:
foreach (var item in _SelectManyIteratora) { }
然后您将看到正在执行的方法。在开始迭代返回值之前,方法中的任何代码都不会执行。每次代码点击yield-return
语句时,控件将返回到迭代它的代码,直到该代码请求下一项-此时它将跳回方法,就在yield-return
语句之后,所有局部变量都与之前处于相同的状态
有关详细信息: