yield-return

What is the proper pattern for handling Enumerable objects with a yield return?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-30 18:57:11
问题 Does there exist a standard pattern for yield returning all the items within an Enumerable? More often than I like I find some of my code reflecting the following pattern: public IEnumerable<object> YieldReturningFunction() { ... [logic and various standard yield return] ... foreach(object obj in methodReturningEnumerable(x,y,z)) { yield return obj; } } The explicit usage of a foreach loop solely to return the results of an Enumerable reeks of code smell to me. Obviously I could abandon the

What concrete type does 'yield return' return?

早过忘川 提交于 2019-12-30 08:26:10
问题 What is the concrete type for this IEnumerable<string> ? private IEnumerable<string> GetIEnumerable() { yield return "a"; yield return "a"; yield return "a"; } 回答1: It's a compiler-generated type. The compiler generates an IEnumerator<string> implementation that returns three "a" values and an IEnumerable<string> skeleton class that provides one of these in its GetEnumerator method. The generated code looks something like this*: // No idea what the naming convention for the generated class is

Recursion with yield return elements order in tree

家住魔仙堡 提交于 2019-12-30 06:12:09
问题 I have a recursive function that returns all subtree nodes, given the starting root node. private IEnumerable<Node> getAllNodesRecursively(Node subnode) { foreach (Node node in subnode.Nodes) getAllNodesRecursively(node); yield return subnode; } For the following tree structure: A | +--B | +--C | | | +--D | +--E When I try to iterate as such: foreach (Node n in getAllNodesRecursively(a)) { Console.WriteLine(n); } the function returns the only the A value. I wish to use yield-return with

Yield return deferred iteration problems

不想你离开。 提交于 2019-12-24 15:19:06
问题 I know that yield return takes advantage of lazy loading but I'm wondering if I might be misusing the iterator or quite possibly need a refactor. My recursive iterator method returns all the ancestors of a given PageNode including the pageNode itself. public class PageNodeIterator { //properties and constructor left out for brevity public IEnumerable<IPageNode> ancestorsOf(IPageNode pageNode) { if(pageNode == null) throw new ArgumentNullException(("pageNode")); if (pageNode.url !=

yield return vs. return IEnumerable<T>

只谈情不闲聊 提交于 2019-12-21 07:15:32
问题 I've noticed something curious about reading from an IDataReader within a using statement that I can't comprehend. Though I'm sure the answer is simple. Why is it that whilst inside the using (SqlDataReader rd) { ... } if I directly perform a yield return the reader stays open for the duration of the read. But if I perform a direct return calling a SqlDataReader extension method (outlined below) that the reader closes before the enumerable can be actualized? public static IEnumerable<T>

Yield return inside usings

感情迁移 提交于 2019-12-20 18:30:04
问题 If I recall correctly that when I used yield inside using SqlConnection blocks I got runtime exceptions. using (var connection = new SqlConnection(connectionString)) { var command = new SqlCommand(queryString, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); // Call Read before accessing data. while (reader.Read()) { yield reader[0]; } // Call Close when done reading. reader.Close(); } Those problems were solved when I replaced yield by a List where I added

Performance comparison of IEnumerable and raising event for each item in source?

馋奶兔 提交于 2019-12-19 04:12:44
问题 I want to read big binary file containing millions of records and I want to get some reports for the records. I use BinaryReader to read (which I think has the best performance in readers) and convert read bytes to data model. Due to the count of records, passing model to the report layer is another issue: I prefer to use IEnumerable to have LINQ functionality and features when developing the reports. Here is sample data class: Public Class MyData Public A1 As UInt64 Public A2 As UInt64

Yield return from a try/catch block [duplicate]

帅比萌擦擦* 提交于 2019-12-19 02:37:08
问题 This question already has answers here : yield return with try catch, how can i solve it (10 answers) Closed 6 years ago . As Eric Lippert described in this article, yield return is not allowed within try/catch clauses. Is there a nice way I could get something like this, without having to write my own IEnumerator by hand: public IEnumerable<Data> GetData() { var transaction = Session.BeginTransaction()); try { IQuery q = CreateQuery(session); foreach (var result in q.Enumerable()) yield

yield returns within lock statement

一世执手 提交于 2019-12-17 19:37:19
问题 if i have a yield return in a lock statement does the lock get taken out on each yield (5 times in the example below) or only once for all the items in the list? Thanks private List<string> _data = new List<string>(){"1","2","3","4","5"}; private object _locker =new object(); public IEnumerable<string> GetData() { lock (_locker) { foreach (string s in _data) { yield return s; } } } 回答1: Edit: This answer was wrong, but I can't delete it as it was marked as correct. Please see @Lockszmith's

Using IEnumerable without foreach loop

二次信任 提交于 2019-12-17 07:17:16
问题 I've gotta be missing something simple here. Take the following code: public IEnumerable<int> getInt(){ for(int i = 0; i < 10; i++){ yield return i; } } I can call this with: foreach (int j in obj.getInt()){ //do something with j } How can I use the getInt method without the foreach loop: IEnumerable<int> iter = obj.getInt(); // do something with iter ?? Thanks. EDITS For those wondering why I'd want this. I'm iterating two things: IEnumerator<int> iter = obj.getInt().GetEnumerator(); foreach