0

I questioned this myself after noticing this question.

for (var i = 0; i < coords.length; ++i) {
    this["marker"+i] = "some stuff";
}
  • this is an array of strings with indexes "marker"+i.

how does interpreter handle such array naming? any consequences, such as waste of memory, etc?

Community
  • 1
  • 1
Ziai
  • 41
  • 4
  • 8
    That question is about Javascript. You're now asking something about C#, but it's not clear what... in particular, that just won't work with an *array*... it would work with a type using a custom indexer, but it's still not clear what you're asking about, especially in terms of an "interpreter". – Jon Skeet Apr 25 '14 at 14:14
  • 1
    And you know the question you linked is from Javascript ? and it is a bad idea ? – Habib Apr 25 '14 at 14:15
  • 3
    Are you looking for `Dictionary`? – Sriram Sakthivel Apr 25 '14 at 14:16

2 Answers2

2

The question you referenced was in javascript, and C# is a completely different beast.

In C#, the index must be an integer, as it is a reference to the position in the array. If you want to use something like "marker" + i as your access point to the data, then you'll need to use a key value pair data type similar to Dictionary.

krillgar
  • 12,133
  • 6
  • 45
  • 83
0
Dictionary<String, String> myDict = new Dictionary<String, String>();
for (var i = 0; i < coords.length; ++i) 
{
    myDict.Add("marker"+i, "some stuff");
}
Sriram Sakthivel
  • 69,953
  • 7
  • 104
  • 182
Anthony Raymond
  • 6,915
  • 6
  • 41
  • 56