Say I have such a method (which has been stolen by John Skeet from the previous SEO answer):
public static IEnumerable & lt; TSource & gt; Duplicate By & lt; TSource, TKey & gt; (This IEnumerable & lt; TSource & gt; source, function & lt; TSource, TKey & gt; key selector) {HashSet & lt; TKey & gt; Scenes = NewHashet & lt; TKey & gt; (); Foreign object (source in source) {// to generate it if the key is not actually included - that is // already in the set if (see KA.Ed. (key selector)) {Yield returns element; }}}
In this method I have a hashase that is used to capture keys that have been seen. If I use this method something like this
list & lt; String & gt; Wire = new list & lt; String & gt; {"1", "1", "2", "3"}; & Lt; String & gt; Some Unique = Strings Duplicates By (S =>. Take (2);
This will only calculate the first 2 items in the string list. But how does garbage collection collects Kashmir consoles? Since the yield only prevents the execution of the law, if the method is expensive then how can I ensure that I would like to settle things properly?
The compiler generates a hidden class to implement this code, including a super-secret name Is: "d__0`2" until you collect the class object, you see that the source veerate becomes the field of that class, ensuring that they can not collect garbage.
The class INImMeter
In order to see the use of Ildasm.exe or reflector, it will give you some insight into the hidden cost of syntactic sugar. Iterators are not cheap.
Comments
Post a Comment