Skip to main content

Favorite C#.Net Interview Question: Numeric Conversions

I like to ask a wide variety of technical questions targeted to the candidate's seniority during interviews. My favorite C# question focuses on converting an Int64 into an Int32. Hopefully, this isn't something you need to perform often (poor design) but occasionally we have to clean up after the other guy, right? ;-) (If you interview with me and are reading this post-consider it a gimme...and hopefully you learned something anyway.)

Situation: We have an Int64 "b" I need to convert into an Int32 "a". How should we set a = b in the safest manner (i.e. no data loss)? Here are some options:



  • Line 15 does not compile throwing a "Cannot implicitly convert type 'long' to 'int'. " exception.
  • Line 17 properly raises an OverflowException.
  • Line 19 does _not_ throw an exception. a's value incorrectly becomes 1569325055.
  • Line 21 properly raises an OverflowException.
In my opinion, the style in Line 17 is the best because it leverages the Framework to do most of the work. This way, if the Framework implementation ever changes, my code will automatically benefit. Also, most developers aren't familiar with the checked construct (2nd best option, IMO) so it looks a bit out of place.

Many developers unknowingly choose Line 19. Bugs introduced with this code can be some of the nastiest to track down. Be safe out there! Convert explicitly using the Framework.

Good web links for conversion:
Jeff Adkin's Data Conversion posting
Explicit Numeric Conversions Table (C# Reference)

Comments

Popular posts from this blog

Starting a New Blog with my Cousin

On 9/15/2006, my cousin (he's really a 2nd cousin many times removed...I'm not convinced we're actually related ;-) John Ingrisano and I will launch a new blog targeted to the small business owner. He'll provide most of the content and I will provide the technical and marketing expertise. Ok, ok, starting a blog isn't rocket science but we're aiming to [eventually] derive some sort of profit from this venture, so I plan on doing this right. The two blogs I currently maintain live on the free Blogger.com and LiveJournal.com. I'll be covering the adventure here on Effective Thoughts--providing insight as to what worked and what didn't. To start off, our first activities are: Choose a name and a theme Find a host Help John learn the ins and outs of blogging Implement the site: look/feel, about/bio, 1st content, announcements, etc. Understand marketing best practices To this end, here is some research I compiled: Finding a host http://en...

Switching the Parents to Ubuntu...?

I spent a half hour or so recently on the phone walking my Mom through a technical issue. Tentatively, I diagnosed her issue as a hard drive failure. She brought it over on her last visit and sure enough, the Dell XPS 450 from circa 1999 sounds like a bad coin-operated laundry at full capacity. I was aghast to discover she's running Windows 98. Ugh. Also, her recovery disk is just that--for recovery. I don't believe I'll be able to re-install Win98 on a new hard drive. That, coupled with the end of Microsoft (and Dell) support for Win98, got me thinking about Linux. (and she's not intense about her computing needs...and she doesn't want to spend much money...) I've been reading good things about switching one's parents to Ubuntu. Any thoughts out there?