What is the purpose of FOREACH
what is the purpose of FOREACH....example I need
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jul 26, 2013, 2:21 AM EDT
ex:-
using System;
class Program
{
static void Main()
{
// Use a string array to loop over.
string[] ferns =
{
"Psilotopsida",
"Equisetopsida",
"Marattiopsida",
"Polypodiopsida"
};
// Loop with the foreach keyword.
foreach (string value in ferns)
{
Console.WriteLine(value);
}
Console.ReadLine();
}
}
VulpesRank #55 · 29.2K reputationPosted Jul 26, 2013, 2:27 AM EDT
However, you can get around this by using the LINQ extension method Reverse() to reverse the order of the collection.
As it's doing more behind the scenes, foreach is also slightly slower than for, though the difference is unimportant in the vast majority of applications.
om prakashRank #5,816 · 285 reputationPosted Jul 26, 2013, 3:31 AM EDT
om prakashRank #5,816 · 285 reputationPosted Jul 26, 2013, 3:33 AM EDT