bool myBool = true;
byte myByte;
- This conversion runs
myByte = Convert.ToByte(myBool); - This conversion does not run
myByte = (byte)myBool;
For a newbie(me): why are the above different?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Convert.ToByteis a method – it can do whatever it wants to, probably along the lines of:A cast is a language-level operation. It requires that either the language knows about the conversion itself, or that one of the types involved has a user-defined conversion with the right input and output types. Neither of these is the case when converting from
booltobyte.Basically, the language doesn’t define what that cast should mean, so the compiler prohibits it.