yield-return

Proper use of 'yield return'

心已入冬 提交于 2019-12-17 02:51:47
问题 The yield keyword is one of those keywords in C# that continues to mystify me, and I've never been confident that I'm using it correctly. Of the following two pieces of code, which is the preferred and why? Version 1: Using yield return public static IEnumerable<Product> GetAllProducts() { using (AdventureWorksEntities db = new AdventureWorksEntities()) { var products = from product in db.Product select product; foreach (Product product in products) { yield return product; } } } Version 2:

Infinite state machine with an IDisposable

半腔热情 提交于 2019-12-12 11:20:46
问题 Lets say I have an infinite state machine to generate random md5 hashes: public static IEnumerable<string> GetHashes() { using (var hash = System.Security.Cryptography.MD5.Create()) { while (true) yield return hash.ComputeHash(Guid.NewGuid().ToByteArray()); } } In the above example I use an using statement. Will the .Dispose() method ever be called? CQ, will the unmanaged resources ever be freed? For example, if I use the machine as follows: public static void Test() { int counter = 0; var

Yielding with an IDisposable resource

浪子不回头ぞ 提交于 2019-12-12 10:33:10
问题 Is there a proper way to yield through a disposable resource? The returned objects are IDisposable, but the element it is iterating through is. Here is an example: public static IEnumerable<T> Fetch(IEnumerable<Guid> ids) { using (var client = new CouchbaseClient()) { yield return ids.Select(s => s.ToString()); } } Right now, calling this would not dispose the using resource obtained. I know I can just to a ToList and return it all at once, but is there a way to handle this "properly", or do

Enumerator disposal when not using using, foreach or manually calling Dispose()

 ̄綄美尐妖づ 提交于 2019-12-12 07:13:50
问题 I'm using yield return to iterate over an SqlDataReader 's records: IEnumerable<Reading> GetReadings() { using (var connection = new SqlConnection(_connectionString)) { using (var command = new SqlCommand(_query, connection)) { connection.Open(); using (var reader = command.ExecuteReader()) { while (reader.Read()) { yield return new Reading { End = reader.GetDateTime(0), Value = reader.GetDouble(1) }; } } connection.Close(); } } } I'm then using an adapted version of this accepted answer to

Using Yield Within While Loop (PriorityQueue Implementation)

跟風遠走 提交于 2019-12-12 05:28:58
问题 I have been playing around with various implementations of a PriorityQueue class lately, and I have come across some behavior I do not fully understand. Here, is a snippet from the unit test I am running: PriorityQueue<Int32> priorityQueue = new PriorityQueue<Int32>(); Randomizer r = new Randomizer(); priorityQueue.AddRange(r.GetInts(Int32.MinValue, Int32.MaxValue, r.Next(300, 10000))); priorityQueue.PopFront(); // Gets called, and works correctly Int32 numberToPop = priorityQueue.Count / 3;

Processing huge data from sql server

白昼怎懂夜的黑 提交于 2019-12-11 17:52:35
问题 I have a stored procedure (SQL Server 2016) which currently returns 100K to 200K rows based on the parameters to that SP. Each row can be a size of 100KB to 200KB. So total size can be around 10GB to 20GB. My client(background job) has to call this SP and process all rows and send it to another client. What is the best approach to handle such scenarios? Currently I am thinking of using streaming enumerator using yield. Get the record whenever the 'datareader.Read()' read a row and process it

How to return ILookup directly (without using a Dictionary->ILookup converter)

风格不统一 提交于 2019-12-11 09:46:31
问题 (Converting a Dictionary to ILookup is not very nice: How do I convert a Dictionary to a Lookup? ) I want to have an interface for my container class with the following method: ILookup<Type, Entry> Query ( IEnumerable<QueryClause> query ); Each query clause specifies which and how many (and some more details) of a special kind of Entry should be taken out of the underlying container. My implementation currently looks something like this: var result = new Dictionary<Type, List<Entry>>();

yield return new WaitForSeconds(2) destroys function

巧了我就是萌 提交于 2019-12-11 04:08:44
问题 I decided to create a little memory like game, to learn game development with unity3d. The game should wait 2 seconds after a player clicked to cards, before it flips the cards back. The yield return new WaitForSeconds(2) -statement should be perfect for this, but it has the effect, that no line of the function is executed. Here is my code: This builds the card grid (with buttons) and calls a function to flip the card, when the card is clicked. Card card = grid[i, j]; if (GUILayout.Button(new

How do I get every combination of letters using yield return and recursion?

≯℡__Kan透↙ 提交于 2019-12-10 16:08:51
问题 I have several lists of strings like so, from a possible list of several dozen: 1: { "A", "B", "C" } 2: { "1", "2", "3" } 3: { "D", "E", "F" } These three were only picked as an example, and the user can pick from several dozen similar lists with varying number of elements. For another example, this is also a perfectly valid selection for a user: 25: { } // empty 4: { "%", "!", "$", "@" } 16: { "I", "a", "b", "Y" } 8: { ")", "z", "!", "8" } What I want to do is get every combination of

Code after yield return is executed

十年热恋 提交于 2019-12-10 12:38:25
问题 Consider the following example: class YieldTest { static void Main(string[] args) { var res = Create(new string[] { "1 12 123", "1234", "12345" }); } static IEnumerable<int> Create(IEnumerable<string> strings) { foreach(string s in strings) { yield return s.Length; if(s.Contains(' ')) { string[] tokens = s.Split(' '); foreach(string t in tokens) { yield return t.Length; } } } } } The call to Create returns {8, 1, 2, 3, 4, 5}. What really confuses me is that the code after the yield return