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
-
Human readable code over compiler friendly code every day of the week.
I wouldn't use this construct at all :Code:while ((data=sr.ReadLine()) !=null)
Code:++i
Code:if (1==i)
Not that I would use a variable called "i" though.Comment
-
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 :Code:while ((data=sr.ReadLine()) !=null)
Code:++i
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:
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
Will work inside IR35. Or for food.Comment
-
we should probably all be writing i=i+1 for clarity instead of ++i or i++.
It should beCode:i+=1
Comment
-
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
-
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
Contractor Services
CUK News
- Streamline Your Retirement with iSIPP: A Solution for Contractor Pensions Sep 1 09:13
- Making the most of pension lump sums: overview for contractors Sep 1 08:36
- Umbrella company tribunal cases are opening up; are your wages subject to unlawful deductions, too? Aug 31 08:38
- Contractors, relabelling 'labour' as 'services' to appear 'fully contracted out' won't dupe IR35 inspectors Aug 31 08:30
- How often does HMRC check tax returns? Aug 30 08:27
- Work-life balance as an IT contractor: 5 top tips from a tech recruiter Aug 30 08:20
- Autumn Statement 2023 tipped to prioritise mental health, in a boost for UK workplaces Aug 29 08:33
- Final reminder for contractors to respond to the umbrella consultation (closing today) Aug 29 08:09
- Top 5 most in demand cyber security contract roles Aug 25 08:38
- Changes to the right to request flexible working are incoming, but how will contractors be affected? Aug 24 08:25
Comment