Can a debugger tell me the foreach loop account / status?

Suppose I have the following code:

List<SomeObject> someObjects = ReturnListWithThousandsOfObjects();

foreach(SomeObject someobject in someObjects)
{
   DoSomething.With(someObject);
}

And also suppose that after a minute of work I set a breakpoint at DoSomething.With(someObject);.

The debugger is great for me. But now I want to know at what point I am in my iteration of the list (suppose the list is unordered / does not have a key).

Is there a way for the debugger to say: " foreachcompleted 532 out of 2321 iterations"?

+7
source share
7 answers

As debugging one of them there is no index method?

i.e.

quickwatch - someObjects.indexOf (someObject);

Added - Sorry if a little short.

Guffa, , EqualComparer (, GetHashCode/Equals).

    public class ATest
    {
        public int number { get; set; }
        public int boo { get; set; }
        public ATest()
        {

        }
    }

    protected void Go()
    {
        List<ATest> list = new List<ATest>();

        foreach(var i in Enumerable.Range(0,30)) {
            foreach(var j in Enumerable.Range(0,100)) {
                list.Add(new ATest() { number = i, boo = j });                  
            }
        }

        var o =0; //only for proving concept.
        foreach (ATest aTest in list)
        {               
            DoSomthing(aTest);
            //proof that this does work in this example.
            o++;
            System.Diagnostics.Debug.Assert(o == list.IndexOf(aTest));
        }

    }
+8

Visual Studio, IDE :

, "Count Count". ( " " 0 , " " ). Hit Count ( reset).

. , , , , , , , .

, , / ( , ). , "" , , , , reset .

+5

, , , for?

List<SomeObject> someObjects = ReturnListWithThousandsOfObjects();


for(int someObj= 1; someObj <= someObjects.Count; someObj++)
{
   Console.WriteLine(string.Format("{0} of {1} iterations", someObj, someObjects.Count));
   DoSomething.With(someObject[someObj]);
}

foreach for, for , .

+2

, . , , , , , .

, :

int count = 0;
List<SomeObject> someObjects = ReturnListWithThousandsOfObjects();

foreach(SomeObject someobject in someObjects)
{
     count++;
     DoSomething.With(someObject);
}

,

+1

, Action, Action sideFold, , .

0

Visual Studio Immediate, # . List.IndexOf(), :

querying the foreach iteration index in the Immediate Window

0

, . [ VS 2017]:

  1. foreach
  2. "".
  3. : $FUNCTION {list.Items.IndexOf(item)} "list" - , "item" - .
0

All Articles