• 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

    #21
    Of course one reason you have to put return values into a variable rather than use them directly is that people writing the methods often return null when they should have thrown an exception so you need to check the value before you use it. This gets on my tits.
    While you're waiting, read the free novel we sent you. It's a Spanish story about a guy named 'Manual.'

    Comment


      #22
      Thanks so much for the replies.

      Minestrone, sorry hasty on the neg, I think the some total of your contributions was worth a +ve, even if bits of your code didn't do what mine did. . Will correct in due course.

      P.S. There is a LOT more data manipulation code in the loop I just cleared it out to make clear the bit I was asking about. Performance is important but not THAT important in this situation.

      Thank you all again.
      Last edited by Scrag Meister; 9 November 2012, 08:06.
      Never has a man been heard to say on his death bed that he wishes he'd spent more time in the office.

      Comment


        #23
        Human readable code over compiler friendly code every day of the week.
        Totally agree, I *hate* it when people try to something clever in a couple of lines of code which then take ages to debug.

        I wouldn't use this construct at all :
        Code:
        while ((data=sr.ReadLine()) !=null)
        But

        Code:
        ++i
        or

        Code:
        if (1==i)
        I think these are easily human readable.

        Not that I would use a variable called "i" though.

        Comment


          #24
          Originally posted by RasputinDude View Post
          Totally agree, I *hate* it when people try to something clever in a couple of lines of code which then take ages to debug.

          I wouldn't use this construct at all :
          Code:
          while ((data=sr.ReadLine()) !=null)
          But

          Code:
          ++i
          or

          Code:
          if (1==i)
          I think these are easily human readable.

          Not that I would use a variable called "i" though.
          Old habits die hard, always use i or j etc as simple loop counters.
          Never has a man been heard to say on his death bed that he wishes he'd spent more time in the office.

          Comment


            #25
            I thought I'd see what VC10 would do, and in debug so no optimisations:

            Code:
                ++i;
            3D60FBD0  mov         edx,dword ptr [i]  
            3D60FBD3  add         edx,1  
            3D60FBD6  mov         dword ptr [i],edx  
                i++;
            3D60FBD9  mov         eax,dword ptr [i]  
            3D60FBDC  add         eax,1  
            3D60FBDF  mov         dword ptr [i],eax  
                i=i+1;
            3D60FBE2  mov         ecx,dword ptr [i]  
            3D60FBE5  add         ecx,1  
            3D60FBE8  mov         dword ptr [i],ecx
            All three produce exactly the same code. So on that basis, we should probably all be writing i=i+1 for clarity instead of ++i or i++.
            Will work inside IR35. Or for food.

            Comment


              #26
              we should probably all be writing i=i+1 for clarity instead of ++i or i++.
              Wash your mouth out.

              It should be
              Code:
              i+=1
              and I'll brook no argument.

              Comment


                #27
                Originally posted by Scrag Meister View Post
                Old habits die hard, always use i or j etc as simple loop counters.
                Likewise, in my mind it is far easier to read and the better approach for loop of that nature.

                I think "while( (data = sr.ReadLine()) != null )" is fine too, it's a common pattern and therefore easy enough to read.

                Comment


                  #28
                  Originally posted by Jaws View Post
                  Likewise, in my mind it is far easier to read and the better approach for loop of that nature.

                  I think "while( (data = sr.ReadLine()) != null )" is fine too, it's a common pattern and therefore easy enough to read.
                  So with that in mind I could do this too? i.e. Assign to my data string all within the same statement.

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

                  // Process the string "data"

                  }

                  At this rate I should be able to squeeze the whole process into 3 lines LOL. Not really but this is very interesting to me. Thanks.
                  Never has a man been heard to say on his death bed that he wishes he'd spent more time in the office.

                  Comment


                    #29
                    Don't forget to use the comma operator:

                    Console.WriteLine(iRows++ + " - " + (data = sr.ReadLine(), somethingElse = thing.SomethingElse(), iRows<<=2, someExtremelyLargeAndSlowOperation() ));

                    Chuck in some lambda functions (can C# do that?) and you can write an entire program in one statement.
                    Will work inside IR35. Or for food.

                    Comment


                      #30
                      Starting to look like an entry for the IOCCC (The International Obfuscated C Code Contest)

                      Comment

                      Working...
                      X