-3

From what I have learned so far, there is no index in foreach loops. So, can anyone tell me what is this index field, when inspecing object in visual studio? enter image description here

Clearly, it is not index as it would be in for loop, because that number is higher than collection count.

Here is the code, I am inspecing group variable.

static string LookAndSay (string input) {
            var groups = new Regex("([0-9])\\1*").Matches(input);
            var result = String.Empty;

            foreach (var group in groups)
                result += String.Concat(group.ToString().Length, group.ToString()[0]);
            return result;
        }
Jānis
  • 1,326
  • 16
  • 29
  • 3
    Which object are you inspecting? – RickL Dec 14 '15 at 16:39
  • It is `var group in groups` where groups is Regex group collection. I'm inspecting `group` – Jānis Dec 14 '15 at 16:41
  • 1
    It's not visible in the screen shot _which_ object you are inspecting. This `Index` is a property of that object and not related to your `foreach` in any way. – René Vogt Dec 14 '15 at 16:41
  • Is it the `Index` property of the object you are inspecting, e.g. http://referencesource.microsoft.com/#System/regex/system/text/regularexpressions/RegexCapture.cs,d33b0131e21561d5,references ? – CompuChip Dec 14 '15 at 16:42
  • Really hard to tell from a heavily constrained screenshot. However the inspector gives you *all* information to quickly learn yourself at the first place. – Ondrej Tucny Dec 14 '15 at 16:42
  • @HimBromBeere I'm not using LINQ anywhere. – Jānis Dec 14 '15 at 16:46
  • @Him: I really hope you didn't downvote on such a false assumption.. – TaW Dec 14 '15 at 16:46

3 Answers3

2

Because you're looping on groups which consists of System.Text.RegularExpressions.Match than the Index property is part of that class.

Explanation from MSDN

The position in the original string where the first character of the captured substring is found.(Inherited from Capture.)

Orel Eraki
  • 10,908
  • 3
  • 23
  • 34
0

It is the current enumerator of class that implements IEnumerable.

See here for more details:

How do you get the index of the current iteration of a foreach loop?

The foreach is for iterating over collections that implement IEnumerable. It does this by calling GetEnumerator on the collection, which will return an Enumerator.

This Enumerator has a method and a property:

  • MoveNext()
  • Current

Current returns the object that Enumerator is currently on, MoveNext updates Current to the next object.

Obviously, the concept of an index is foreign to the concept of enumeration, and cannot be done.

Because of that, most collections are able to be traversed using an indexer and the for loop construct.

I greatly prefer using a for loop in this situation compared to tracking the index with a local variable.

Community
  • 1
  • 1
Ian P
  • 12,298
  • 6
  • 42
  • 68
0

So, Index is a property of whatever object you are inspecting. This may or may not have anything to do with foreach--we can't tell. But for what it's worth: foreach operates by calling an inherited GetEnumerator method in whatever the collection is you are looping through. The GetEnumerator method tells it what object to get next (internally, it might use that Index value), returns the Current object, and specifies what type the current object is. More on that here: https://msdn.microsoft.com/en-us/library/aa288257(v=vs.71).aspx

Matthew
  • 3,560
  • 1
  • 19
  • 44