Hi. Let's say I have a method in a foreach statement, for example:

foreach(string str in GetStrings())
...

Does the method (GetStrings() in our example) get called once or once per iteration?

Recommended Answers

All 2 Replies

Hi,

It gets called just once.

Jon

Only ones of course.
Different would be something like that:

string[] array = GetString();
//method GetString returns an array of strings
//you you go through a foreach loop like it:
foreach(string str in array)
{
   here you can loop through str string!
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.