Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can I return an array with foreach loop?

So I tried to return my array with a foreach loop… but no results… If someone can help me return an array, it will help me greatly.
Here is the test code of how I tried to do that:

int[] ints = { 1, 2, 3 };

foreach(int i in ints)
{
    return ints[i];
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Like @lee Taylor mentioned. It is weird to do this, but yield is what you want.

int[] ints = { 1, 2, 3 };

foreach(int i in ints)
{
    yield return i;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading