Tag: yield

Objective-C到C#的yield关键字是否有任何并行

我第一次在C#中看到yield关键字时,我认为“糟糕的是如何破解语言”。 从那时起成长并实际使用该语言我发现表达状态逻辑非常简单,我想在其他开发平台中使用类似的方法。 我正在探索Objective-C的一些支持实用程序。 是否有类似Objective-C的C#yield关键字?

在Coroutine中“yield return 0”和“yield return null”之间有什么区别?

我很新,对“ yield ”有点困惑。 但最后我理解使用WaitForSeconds它是如何工作的 但我看不出“ yield return 0 ”和“ yield return null ”之间的区别。 他们都在等待下一帧执行? 对不起,我的英语不好。 非常感谢你。

C#中的收益率是否是线程安全的?

我有以下代码: private Dictionary items = new Dictionary; public IEnumerable Keys { get { foreach (object key in items.Keys) { yield return key; } } } 这是线程安全的吗? 如果不是,我必须lock循环或yield return ? 这就是我的意思: Thread1访问Keys属性,而Thread2将一个项添加到基础字典。 Thread1是否受Thread2的影响?

并发或性能返回列表时yield收益的好处

我想知道使用yield return而不是返回列表是否存在任何并发(现在或将来)或性能优势。 请参阅以下示例 处理方法 void Page_Load() { foreach(var item in GetPostedItems()) Process(item); } 使用收益率回报 IEnumerable GetPostedItems() { yield return Item1.Text; yield return Item2.Text; yield return Item3.Text; } 返回一个清单 IEnumerable GetPostedItems() { var list = new List(); list.Add(Item1.Text); list.Add(Item2.Text); list.Add(Item3.Text); return list; }

缓存IEnumerable

public IEnumerable ListModules() { foreach (XElement m in Source.Descendants(“Module”)) { yield return new ModuleData(m.Element(“ModuleID”).Value); } } 最初上面的代码很棒,因为如果不需要,就不需要评估整个集合。 但是,一旦枚举了所有模块,在没有更改时重复查询XDocument会变得更加昂贵。 所以,作为绩效改进: public IEnumerable ListModules() { if (Modules == null) { Modules = new List(); foreach (XElement m in Source.Descendants(“Module”)) { Modules.Add(new ModuleData(m.Element(“ModuleID”).Value, 1, 1)); } } return Modules; } 如果我反复使用整个列表,那就太好了,但不是那么好。 是否存在中间点,我可以在整个列表被迭代之前返回,然后缓存它并将缓存提供给后续请求?

调用包含yield的方法后,不会执行代码行

考虑以下方法: IEnumerable GetTimes(int count) { for (int i = 0; i < count; i++) yield return DateTime.Now; yield break; } 现在,我想称之为: var times = GetTimes(2); Console.WriteLine(“First element:” + times.Take(1).Single().ToString()); Console.WriteLine(“Second element:” + times.Skip(1).Take(1).Single().ToString()); Console.WriteLine(“Third element:” + times.Skip(2).Take(1).Single().ToString()); Console.WriteLine(“Finished…”); 但最后一行代码永远不会运行。 为什么?

将这个foreach产量重写为linq产量?

假设我有以下代码(上下文缩小以保持问题范围有限) public static IEnumerable GetThemColors(){ var ids = GetThePrimaryIds(); foreach (int id in ids){ yield return GetColorById(id); } ids = GetTheOtherIds(); foreach (int id in ids){ yield return GetOtherColorsById(id); } } 我想把它们改写成这样的东西(当然不会编译 public static IEnumerable GetThemColors(){ GetThePrimaryIds().Select(id=>yield return GetColorById(id)); GetTheOtherIds().Select(id=>yield return GetOtherColorsById(id)); } 关键点在于,在我的第一个片段中,我有两个foreach枚举器屈服,我不知道如何在linq中做什么而不会丢失我的延迟加载function。

使用IEnumerable 和Linq-To-Sql时如何防止内存溢出?

这个问题与我之前的一个问题有关 这是我目前的代码 IEnumerable Get() { while(//get implementation yield return new Shape(//… } void Insert() { var actual = Get(); using (var db = new DataClassesDataContext()) { db.Shapes.InsertAllOnSubmit(actual); db.SubmitChanges(); } } 我的内存溢出,因为IEnumerable太大了。 我该如何预防呢?

使用委托从C#调用IronRuby

是否可以使用委托作为参数从C#调用IronRuby方法,以便yield可以工作? 以下给出了错误数量的参数(1表示0)exception。 Action action = Console.WriteLine; var runtime = Ruby.CreateRuntime(); var engine = runtime.GetEngine(“rb”); engine.Execute(@” class YieldTest def test yield ‘From IronRuby’ end end “); object test = engine.Runtime.Globals.GetVariable(“YieldTest”); dynamic t = engine.Operations.CreateInstance(test); t.test(action);

序列化和Yield语句

是否可以序列化包含yield语句的方法(或包含此类方法的类),以便在对类进行重新水化时,保留生成的迭代器的内部状态?