Visual Studio C# Question About Casting and Type Conversion via /r/learnprogramming


Visual Studio C# Question About Casting and Type Conversion

I finished learning pythin recently and wanted to move on to something else so I chose C#. I found a course on Udemy and I have some questions.

Earlier, we learn about casting and explicit type conversion. The video shows an example of converting a int variable initalized to 1000. It would use casting to convert it to a byte (which has a max range of 255). image of screen. He runs the code and it returns a 232 which shows that data loss is going on. I understand all this.

Later he attempts to do a conversion of a string (of a number) to a byte. Image in question – but it crashes. Crash Image. The error says "Value was either too large or too small…"

My question is: in example 2, why did it crash? Why didn't it just attempt to convert it with data loss like the first example? Or Why didn't example 1 crash since the number 1000 is larger than the 255 range max?

Submitted July 17, 2017 at 06:06PM by BigB69
via reddit http://ift.tt/2utdv2B

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s