Wrox Programmer Forums
|
BOOK: Professional C# 2008 ISBN: 978-0-470-19137-8
This is the forum to discuss the Wrox book Professional C# 2008 by Christian Nagel, Bill Evjen, Jay Glynn, Morgan Skinner, Karli Watson; ISBN: 9780470191378
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional C# 2008 ISBN: 978-0-470-19137-8 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 1st, 2010, 09:34 PM
Registered User
 
Join Date: Mar 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default "Operators and Casts" questions

The following code is given as an example of where implicit conversion fails (Chapter 6, pg. 149).

Code:
byte value1 = 10;
byte value2 = 23;
byte total;
total = value1 + value2;
Quote:
The problem here is that when you add 2 bytes together, the result will be returned as an int, not as another byte. This is because a byte can contain only 8 bits of data, so adding 2 bytes together could very easily result in a value that cannot be stored in a single byte.
OK, that makes sense, but why does it then allow you to do the same with ints? Replace the byte with int in the preceding code block and it compiles. Couldn't the same "data loss" happen with the sum of two ints?

My second question is about boxing. Say you are calling a custom (overridden) ToString() method on a user-defined struct, if this is still boxed, why is there a performance benefit associated with overriding the ToString() method?

By the way, fantastic book! I've already learned so much.
 
Old March 29th, 2010, 09:25 PM
Authorized User
 
Join Date: Nov 2009
Posts: 26
Thanks: 1
Thanked 1 Time in 1 Post
Default

Hi.

I wanna say sth about your first question.

The book gives you the answer.

It says when you add 2 bytes the result is returned as an integer (an inner behavior of C#). It means that the result of (value1 + value2) is an integer.
So can you assign an integer to a byte? Of course not.Because integer is a larger data type. What is the solution?
You simply need an explicit conversion of int to byte (look at the error) and then assign it to total.
try this:
Code:
byte v1 = 10;
byte v2 = 23;
byte total;
total = (byte)(v1 + v2);
when you add two integers the type of returned value do not change to other types. It is again integer. Because of that,you don't need any conversion.

Hope that helped.

Erfan...
 
Old March 29th, 2010, 10:50 PM
Registered User
 
Join Date: Mar 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply.

Regarding your example code, understood, the code sample without the explicit cast was taken from the book (it was used as an example of where implicit conversion fails).

My point was simply that, why does it protect against overflow by placing the sum in a larger data type for bytes, yet does not do this for int32?

Actually, after thinking about this more, it is probably because for the byte type, C# does not by default check for overflows (therefore it stores their sum in a larger data type). C# allows you to add two int32s into an int32 data type because an overflow throws an exception. I guess the developer is responsible for handling possible run-time overflows.

For example:
Code:
int k = 2147483647;
k++;
throws an exception.

Am I barking up the right tree?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Difficulties with "web.config" and "ASPNETDB" CFRham BOOK: ASP.NET MVC Website Programming Problem Design Solution ISBN: 9780470410950 2 July 3rd, 2010 10:19 AM
How to theme the "Browse" button of "FileUpload" control? varunbwj BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 October 14th, 2009 01:22 AM
Add a CheckBox DataColumn to my DataGridView, Null format: "" or "True" but Error: F ismailc C# 2005 0 September 25th, 2009 04:56 AM
Code not going as planned: "icicle" vs "savedinstancestate" joopthecat BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 3 May 3rd, 2009 03:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.