Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Programming in the Key of C#

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.

Name: Anonymous 2010-02-11 3:05

You are fucking retarded. That is all.

Name: Anonymous 2010-02-11 3:14

>>1
You see the thing where it prints a *?  And the other thing where it does that thing a bunch of times?  Thereby printing a lot of *s?  Yeah.

Name: Anonymous 2010-02-11 3:17

>>1
that example is shit, learn perl.

Name: !iN.MY.aRMs 2010-02-11 3:35

world best buisnes framework+language is:
http://1c.ru/eng/title.htm
http://www.v8.1c.ru/eng/
over 12x faster development than java
use it and save u money

Name: Anonymous 2010-02-11 3:35

So it is coming from the Console.Write("*") obviously, but I don't get why it's repeated so many times in the for loop.  Is it written every time i < MyFriends().Length is evaluated?  Why does it not do this in the foreach loop?

Name: !iN.MY.aRMs 2010-02-11 3:37

in u example:
use static field for "MyFriends" constant

Name: Anonymous 2010-02-11 4:50

Please ensure that your code in in the [code] tags in the future.

Name: Anonymous 2010-02-11 6:11

>>8
Information wants to be free. No tags no fences no collars.

Name: Anonymous 2010-02-11 6:33

>>9
The only crime controversy commits is against its own industry...

Name: Anonymous 2010-02-11 6:44

>>9
Information doesn't like being anthropomorphized.

Name: Anonymous 2010-02-11 7:33

>Is it written every time i < MyFriends().Length is evaluated?  Why does it not do this in the foreach loop?

If you can't even tell the difference between a for loop and a foreach loop, you'd better kill yourself; a for loop will evaluate the second argument against the third one until its satisfied.

Now go learn the basics of any C-like programming language again.

Name: Anonymous 2010-02-11 7:45

>>12
Now go learn the basics of quoting on /prog/.

OHIBT?

Name: Anonymous 2010-02-11 9:26

MyFriends() is impure

Name: Anonymous 2010-02-11 10:47

>>13

It's not in shiichan source and despite people telling me that the CSS of quotes hints at the tag to use, I can't find it.

Name: Anonymous 2010-02-11 11:22

>>15
I am an idiot
fixed.

Name: Anonymous 2010-02-11 12:44

It's not in shiichan source
Is that so?
$ grep "<span class='quote'>" *
post.php:$mesg = preg_replace("/\n&gt; (.+)/i", "\n<span class='quote'>$1</span>", $mesg);
post.php:$mesg = preg_replace("/^&gt; (.+)/i", "<span class='quote'>$1</span>", $mesg);
post.php:$mesg = preg_replace("/<span class='quote'>&gt; (.+)/i", "<span class='quote'><span class='quote'>$1</span>", $mesg);
post.php:$mesg = preg_replace("/<span class='quote'>&gt; (.+)/i", "<span class='quote'><span class='quote'>$1</span>", $mesg);

Name: Anonymous 2010-02-11 13:43

>>1-san
`*' is written every time MyFriends() is called.  In the for loop `*' is written every time the terminating condition i < MyFriends().Length is checked, plus every time MyFriends()[i].IndexOfAny(achSearch) != -1 is checked in the if statement.  In the foreach loop, an iterator is formed, so MyFriends() is only called once.

The point of the example is to show you that even though for and foreach seem like they should do the same thing, they can some times lead to different results, especially if given an impure function call as >>14-san mentioned.

Name: Anonymous 2010-02-11 13:48

>>17
grep > MY > ANUS!

Don't change these.
Name: Email:
Entire Thread Thread List