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.
- 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
-
While you're waiting, read the free novel we sent you. It's a Spanish story about a guy named 'Manual.' -
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
-
Totally agree, I *hate* it when people try to something clever in a couple of lines of code which then take ages to debug.Human readable code over compiler friendly code every day of the week.
I wouldn't use this construct at all :ButCode:while ((data=sr.ReadLine()) !=null)
orCode:++i
I think these are easily human readable.Code:if (1==i)
Not that I would use a variable called "i" though.
Comment
-
Old habits die hard, always use i or j etc as simple loop counters.Originally posted by RasputinDude View PostTotally 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 :ButCode:while ((data=sr.ReadLine()) !=null)
orCode:++i
I think these are easily human readable.Code:if (1==i)
Not that I would use a variable called "i" though.
Never has a man been heard to say on his death bed that he wishes he'd spent more time in the office.Comment
-
I thought I'd see what VC10 would do, and in debug so no optimisations:
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++.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],ecxWill work inside IR35. Or for food.Comment
-
Wash your mouth out.we should probably all be writing i=i+1 for clarity instead of ++i or i++.
It should beand I'll brook no argument.Code:i+=1
Comment
-
Likewise, in my mind it is far easier to read and the better approach for loop of that nature.Originally posted by Scrag Meister View PostOld habits die hard, always use i or j etc as simple loop counters.
I think "while( (data = sr.ReadLine()) != null )" is fine too, it's a common pattern and therefore easy enough to read.Comment
-
So with that in mind I could do this too? i.e. Assign to my data string all within the same statement.Originally posted by Jaws View PostLikewise, 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.
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
-
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
-
Comment
- Home
- News & Features
- First Timers
- IR35 / S660 / BN66
- Employee Benefit Trusts
- Agency Workers Regulations
- MSC Legislation
- Limited Companies
- Dividends
- Umbrella Company
- VAT / Flat Rate VAT
- Job News & Guides
- Money News & Guides
- Guide to Contracts
- Successful Contracting
- Contracting Overseas
- Contractor Calculators
- MVL
- Contractor Expenses
Advertisers

Comment