• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Beginners C#query

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    #11
    Originally posted by minestrone View Post
    Terrible, terrible code.

    I know exactly why it is done though as they are thinking...

    using (StreamReader sr = new StreamReader(args[0]))
    {
    while ( sr.ReadLine() !=null) {
    iRows++;
    Console.WriteLine(iRows + " - " + sr.ReadLine() );
    }
    }

    involves 2 method calls and they figure that dumping the contents of the call into a variable and using that next is quicker. All balls of course but they think that is how it works. See that kind of tulip everyday sadly.
    Or thinking that the first readline reads a line that gets thrown away so they had better keep it. Of course they don't know about peek.

    Though people writing all C syntax languages like they are C annoys me, that idiom is one I'd keep as it's succinct and common enough that people know what it does.
    While you're waiting, read the free novel we sent you. It's a Spanish story about a guy named 'Manual.'

    Comment


      #12
      Originally posted by doodab View Post
      Or thinking that the first readline reads a line that gets thrown away so they had better keep it. Of course they don't know about peek.

      Though people writing all C syntax languages like they are C annoys me, that idiom is one I'd keep as it's succinct and common enough that people know what it does.
      I think you are right...

      Don't know C# but that would be a..

      using (StreamReader sr = new StreamReader(args[0])) {
      String zeString = sr.ReadLine() ;
      while ( zeString !=null) {
      Console.WriteLine(iRows++ + " - " + zeString );
      }
      }

      from me

      Comment


        #13


        while ( sr.Peek() > -1) {
        Console.WriteLine(iRows++ + " - " + sr.ReadLine());
        }
        }


        might terminate quicker
        While you're waiting, read the free novel we sent you. It's a Spanish story about a guy named 'Manual.'

        Comment


          #14
          Did not know of this peek call.

          I would have written...

          Console.WriteLine( new StreamReader(args[0]) );

          ...and told them if they want the splodge of text formatted into some kind of readable text it would be another 2 days work.

          Kerching.

          Comment


            #15
            Originally posted by minestrone View Post
            Not in that context.

            iRows is going to be the same with ++ .

            The result will be the same, but ++iRows and iRows++ are different operations.


            Actually, looking it into the MSIL a bit, it seems that they bother deliver the same results, which shows a real difference between C++ and C#. I'll research further when I have sobered up.

            But I still think that ++iRows is better. ;-)
            Last edited by RasputinDude; 8 November 2012, 21:53. Reason: proving myself wrong. fml.

            Comment


              #16
              Originally posted by RasputinDude View Post
              The result will be the same, but ++iRows and iRows++ are different operations.
              The next line don't care.

              Comment


                #17
                It's not about the next line though, is it? it's about the most efficient method of incrementing something. Granted in C# land, it's not really an issue, but I also spend a lot of time in very memory and processor restricted C/C++ land where every cycle saved is still a bonus.

                Anyway, I have granted above that in C#, it doesn't seem to make much difference, so I'll accept that in this case you are right. In this context, it doesn't matter.

                Comment


                  #18
                  Originally posted by RasputinDude View Post
                  The result will be the same, but ++iRows and iRows++ are different operations.


                  Actually, looking it into the MSIL a bit, it seems that they bother deliver the same results, which shows a real difference between C++ and C#. I'll research further when I have sobered up.

                  But I still think that ++iRows is better. ;-)
                  For a single line statement I think compilers are clever enough to do the same for ++i and i++. But there's still the odd nutter that thinks it's important and moan if you use i++ (which I nearly always do as it's clearer).

                  Of course the language should have been ++C and not C++.
                  Will work inside IR35. Or for food.

                  Comment


                    #19
                    Yup, most compilers are.

                    But I'm old school and would prefer to see:

                    if (1 == i)

                    rather than

                    if (i == 1)


                    And yes, it should have been ++C

                    Comment


                      #20
                      Human readable code over compiler friendly code every day of the week.

                      Comment

                      Working...
                      X