Monday 15 June 2015

c# - Access Enumerator within a foreach loop? -


I have a list class, and I have to return my code to GetEnumerator () Override Counters Class This nominator class has two additional properties which will be updated as an analytics.

For simplicity (this is not the right business case), assume that these properties were CurrentIndex and runningtotal .

I can manually manage these properties within the foreground loop, but I will assign these uses for reuse, and the enumerator is considered to be the right place. / P>

Problem: foreach hides all annamter business, so there is no such way, which is within a pre-statement statement, enter the current anneater so that Can I retrieve my properties? Or will I have to use the dirty old loop for the future, and the calculator has to manipulate itself?

Strictly speaking, I would say that if you really want to do, You will need to call the gatenator and then manually control the calculator with some loops.

Business is required without knowing too much about yourself, you can take advantage of an Iterator function, such as something:

  public stable IEnumerable & lt ; Decimal & gt; Unconscious value (list & lt; decimal & gt; list) {decimal move total = 0 m; Foreach (decimal value in the list) {// If the value is less than 1% of the ongoing number, ignore it if (current total == 0m> value> gt = 0.01M * running total) { RunningTotal + = value; Yield return value; }}}  

Then you can do this:

  list & lt; Decimal & gt; Payment = new list & lt; Decimal & gt; () {123.45 m, 234.56 m, .01 m, 345.67 m, 1.23 m, 456.78 m}; Foreign currency (Daimler big payments in IgnoreSmallValues ​​(payments)) // Keep large payments so that I can remove all small payments in my small bank account. Mwahaha! }  

updated:

OK, so here is my follow-up with what I have said as a "fishing hook" solution . Now, I will add a disclaimer that I can not really think of a good reason to do something like this, but your situation may be different.

The idea is that you simply "fishing hook" is the object (reference type) that you pass in your iterator function, the iterator function connects your fishing hook object, and whenever you have your own If there is a reference outside the code, then its visibility is what is happening to you:

  public class phishing hook {public index index}; Set; } Public Decimal Runtingtotal {get; Set; } Public Func & lt; Decimal, Bullet & gt; Criteria {receive; Set; }} Public stable IEnumerable & lt; Decimal & gt; Fishing hook artation (INN Merebel & lt; Decimal & gt; List, Fish hook hook) {hook.index = 0; Hook Wild Total = 0; Foreign currency (decimal value in the list) {// Hook object can define a benchmark representative, // which determines whether to leave the current value or not (hook.cremitia == faucet) hook Criterion (value)) {hook = Value; Yield return value; Hook.Index ++; }}}  

You will use it like this:

  list & lt; Decimal & gt; Payment = new list & lt; Decimal & gt; () {123.45 m, .01 m, 345.67 m, 234.56 m, 1.23 m, 456.78 m}; Fish hook hook = new fish hook (); Decimal minimum = 0; Hook Classification = x = & gt; X & gt; Minute; // Leave any value equal to the minimum / minimum value of fixed foreach (decimal value in fishing hooks (payment, hook)) {// Minimum Update if (Price & Minimum) Minimum = Values; Console.light line ("index: {0}, value: {1}, running total: {2}", hook. Index, value, hook, running.); } // Results: Output Output: // Index: 0, Price: 123.45, Running Total: 123.45 // index: 1, value: 345.67, Running Total: 469.12 // Index: 2 Value: 456.78, Running total: 925.90 / / we have implemented what is left of the values ​​.01, 234.56, and 1.23  

essentially, fish Pakdnehuk object that gives you some problems iterator. I received this question impression was that you need a way to access the inner workings of the iterator so you're in between can manipulate while running it again, when you run, but if it is not the case, So this solution can be exaggerated for your needs.


1 comment:

  1. शौकिया प्रोग्रामर्स के लिए फुल कलर बिटमैप c # सैंपल कोड बनाएं
    Create Full Color Bitmap

    ReplyDelete