Name: Anonymous 2010-02-11 2:54
Hey guys, I'm going through this C# book and I'm trying to understand every piece of sample code before I move on and this one has me stumped. Here is the output:
Looping through MyFriends using a forLoop:
*********************************************************
Names with Z's: 16
Looping through MyFriends using foreach:
*
Names with Z': 16
And here is the code:
using System;
class ForAndForEach
{
static void Main()
{
int iNamesWithZees = 0;
char[] achSearch = {'z', 'Z'};
Console.WriteLine("Looping through MyFriends using a for loop:");
for (int i = 0; i < MyFriends().Length; i++)
if (MyFriends()[i].IndexOfAny(achSearch) != -1)
iNamesWithZees++;
Console.WriteLine();
Console.WriteLine("Names with Z's: " + iNamesWithZees);
iNamesWithZees = 0;
Console.WriteLine("Looping through myFriends using foreach:");
foreach (string str in MyFriends())
if (str.IndexOfAny(achSearch) != -1)
iNamesWithZees++;
Console.WriteLine();
Console.WriteLine("Names with Z's: " + iNamesWithZees);
}
static string[] MyFriends()
{
Console.Write("*");
return new string[]
{
"Hazem Abolrous", "Wanida Benshoof", "Suzana De Canuto", "Terry Clayton", "Brenda Diaz", "Terri Lee Duffy", "Maciej Dusza", "Charles Fitzgerald",
"Guy Gilbert", "Jossef Goldberg", "Greg Guzik", "Annette Hill", "George Jiang", "Tengiz Kharatishvili", "Rebecca Laszlo", "Yan Li",
"Jose Lugo", "Sandra I. Martinez", "Ben Miller", "Zheng Mu", "Merav Nez", "Deborah Poe", "Amy Rusko", "Vadim Sazanovich", "David So", "Rachel B. Valdez",
"Raja D. Venugopal", "Paul West", "Robert Zare", "Kimberly B. Zimmerman", "Karen Zimrich"
};
}
}
Where the hell is that first row of asterisks coming from? I've narrowed it down to something about incrementing iNamesWithZees in the for loop, but I don't know what.
Looping through MyFriends using a forLoop:
*********************************************************
Names with Z's: 16
Looping through MyFriends using foreach:
*
Names with Z': 16
And here is the code:
using System;
class ForAndForEach
{
static void Main()
{
int iNamesWithZees = 0;
char[] achSearch = {'z', 'Z'};
Console.WriteLine("Looping through MyFriends using a for loop:");
for (int i = 0; i < MyFriends().Length; i++)
if (MyFriends()[i].IndexOfAny(achSearch) != -1)
iNamesWithZees++;
Console.WriteLine();
Console.WriteLine("Names with Z's: " + iNamesWithZees);
iNamesWithZees = 0;
Console.WriteLine("Looping through myFriends using foreach:");
foreach (string str in MyFriends())
if (str.IndexOfAny(achSearch) != -1)
iNamesWithZees++;
Console.WriteLine();
Console.WriteLine("Names with Z's: " + iNamesWithZees);
}
static string[] MyFriends()
{
Console.Write("*");
return new string[]
{
"Hazem Abolrous", "Wanida Benshoof", "Suzana De Canuto", "Terry Clayton", "Brenda Diaz", "Terri Lee Duffy", "Maciej Dusza", "Charles Fitzgerald",
"Guy Gilbert", "Jossef Goldberg", "Greg Guzik", "Annette Hill", "George Jiang", "Tengiz Kharatishvili", "Rebecca Laszlo", "Yan Li",
"Jose Lugo", "Sandra I. Martinez", "Ben Miller", "Zheng Mu", "Merav Nez", "Deborah Poe", "Amy Rusko", "Vadim Sazanovich", "David So", "Rachel B. Valdez",
"Raja D. Venugopal", "Paul West", "Robert Zare", "Kimberly B. Zimmerman", "Karen Zimrich"
};
}
}
Where the hell is that first row of asterisks coming from? I've narrowed it down to something about incrementing iNamesWithZees in the for loop, but I don't know what.