Last active
January 27, 2021 11:45
-
-
Save andresmoschini/5d62722dab751cc4bcadcbae30d9d31d to your computer and use it in GitHub Desktop.
Prueba de Discposables con Yield
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
// https://softwareengineering.stackexchange.com/questions/300242/will-the-database-connection-be-closed-if-we-yield-the-datareader-row-and-not-re | |
Console.WriteLine("Prueba foreach"); | |
foreach (var i in GetItems("Prueba foreach")) | |
{ | |
Console.WriteLine(i); | |
} | |
Console.WriteLine("************************"); | |
Console.WriteLine(""); | |
Console.WriteLine("Prueba link"); | |
var items = GetItems("Prueba link"); | |
var i2 = items.Skip(1); | |
var i3 = i2.Skip(1); | |
var i4 = i3; | |
Console.WriteLine("WL"); | |
Console.WriteLine(items.First()); | |
Console.WriteLine(i2.First()); | |
Console.WriteLine(i3.First()); | |
Console.WriteLine(i3.First()); | |
Console.WriteLine(i4.First()); | |
Console.WriteLine("Esta ejecuta el constructor y el dispose más veces de las que uno podría creer sin pensarlo mucho"); | |
Console.WriteLine("************************"); | |
Console.WriteLine(""); | |
var items3 = GetItems("Prueba ElementAt"); | |
Console.WriteLine(items3.ElementAt(1)); | |
Console.WriteLine("************************"); | |
Console.WriteLine(""); | |
var enumerator = GetItems("Prueba Enumerator").GetEnumerator(); | |
Console.WriteLine(enumerator.Current); | |
enumerator.MoveNext(); | |
enumerator.MoveNext(); | |
Console.WriteLine(enumerator.Current); | |
Console.WriteLine("Esta es la única que no ejecuta el dispose"); | |
Console.WriteLine("************************"); | |
Console.WriteLine(""); | |
IEnumerable<int> GetItems(string title) | |
{ | |
using var disposable = new DisposableExample(title); | |
for (var i = 0; i < 10; i++) | |
{ | |
Console.WriteLine($"Iterating `{title}` #{i}"); | |
yield return i; | |
} | |
} | |
class DisposableExample : IDisposable | |
{ | |
string _title; | |
public DisposableExample(string title) | |
{ | |
_title = title; | |
Console.WriteLine($"Constructor `{_title}`"); | |
} | |
public void Dispose() | |
{ | |
Console.WriteLine($"Dispose `{_title}`"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Prueba foreach | |
Constructor `Prueba foreach` | |
Iterating `Prueba foreach` #0 | |
0 | |
Iterating `Prueba foreach` #1 | |
1 | |
Iterating `Prueba foreach` #2 | |
2 | |
Iterating `Prueba foreach` #3 | |
3 | |
Iterating `Prueba foreach` #4 | |
4 | |
Iterating `Prueba foreach` #5 | |
5 | |
Iterating `Prueba foreach` #6 | |
6 | |
Iterating `Prueba foreach` #7 | |
7 | |
Iterating `Prueba foreach` #8 | |
8 | |
Iterating `Prueba foreach` #9 | |
9 | |
Dispose `Prueba foreach` | |
************************ | |
Prueba link | |
WL | |
Constructor `Prueba link` | |
Iterating `Prueba link` #0 | |
Dispose `Prueba link` | |
0 | |
Constructor `Prueba link` | |
Iterating `Prueba link` #0 | |
Iterating `Prueba link` #1 | |
Dispose `Prueba link` | |
1 | |
Constructor `Prueba link` | |
Iterating `Prueba link` #0 | |
Iterating `Prueba link` #1 | |
Iterating `Prueba link` #2 | |
Dispose `Prueba link` | |
2 | |
Constructor `Prueba link` | |
Iterating `Prueba link` #0 | |
Iterating `Prueba link` #1 | |
Iterating `Prueba link` #2 | |
Dispose `Prueba link` | |
2 | |
Constructor `Prueba link` | |
Iterating `Prueba link` #0 | |
Iterating `Prueba link` #1 | |
Iterating `Prueba link` #2 | |
Dispose `Prueba link` | |
2 | |
Esta ejecuta el constructor y el dispose más veces de las que uno podría creer sin pensarlo mucho | |
************************ | |
Constructor `Prueba ElementAt` | |
Iterating `Prueba ElementAt` #0 | |
Iterating `Prueba ElementAt` #1 | |
Dispose `Prueba ElementAt` | |
1 | |
************************ | |
0 | |
Constructor `Prueba Enumerator` | |
Iterating `Prueba Enumerator` #0 | |
Iterating `Prueba Enumerator` #1 | |
1 | |
Esta es la única que no ejecuta el dispose | |
************************ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment