Home
Username:
Password:
Introduction to Microsoft C# 2008 Tutorials

C# Essentials / Casting pt. 2

Subtitles of the Movie

Now, this video is Casting Part 2. And for this to really make sense I would strongly suggest you go back and watch Casting Part 1 first because in Casting Part 1, I hope I didn't confuse you but I went into the binary to try to let you understand how C# is thinking about these data types and what can happen when we start casting and moving data between different data types and what I want to show you in Part 2 is how this looks and how it will surprise you in a real coding environment. So the way to do this, I'm going to create a couple of variables here with different data types and we're going to move data between them. So the first variable, I'll create a variable named X with a data type of integer and we will set X equal to 40. And so there we go. We created a variable, X, with an integer data type, set that guy equal to 40. Alright? Then let's create a variable Y that is a byte data type and we will set this guy equal to 30. Now, if I try to set X equal to Y, no problem because I'm setting an integer equal to a byte and a byte's smaller than an integer so there's not going to be absolutely no problem moving this data between these data types. If you go back to video Part 1 on Casting, you'll see in the binary the byte has eight bits, the integer has 32 bits. I can easily move the eight bits into the, I was trying to get that to show up for you there, I was trying to get the smaller, narrower data type to go into the larger, wider one. No problems. So if I run this, it's not going to throw me any errors and if I click, it'll actually load this variable. You'll can't really tell it but no errors are being thrown. What if I try to go the other way? If I say Y equals X? Now, when I try to run this, I'm saying that I want this byte to be loaded with this data from this integer type and so when I run this, it says wait a minute dude. I don't want; OK, I'm sorry. I don't want to run the successful build. So it jumps up there and it shows me an error and if I double click the error it takes me to where the problem is and it is telling me I cannot implicitly convert an int to a byte. Why? It's saying, you might lose some data here. But I'm going to say well, you know what? I know what I'm doing so I will cast that. So I'm telling it I know Y is a byte. Let's treat X, let's turn X into a byte and load that data into Y. So I will run this and notice, not a problem. It's says fine with me dude. As long as you know what you're doing. Well, let's change the rules a little bit. Let's go put 400 in our integer and 30 in the byte. Y is the byte, X is the integer. Now we've got an interesting situation because the byte won't hold that much data. Remember our example from Part 1. So what I'm going to do now is I will put a break in here to show you so we'll go look at the value on these things. So I will run this this time and it's not going to throw an error, OK, because I told it what to do. Now, notice, if I mouse over, the X is worth 400, the Y is worth 30, right? But as soon as I move to the next line, I'll hit F11 here. We haven't talked about debugging yet. But notice when I mouse over Y, what it's worth now. It's 144. How did it get to be 144? Go back and watch video Part 1. What happened, when we told it to convert X into a byte, it dropped part of the value. Notice right here, if I click on this, if I just mouse over byte, it's showing my maximum value is 255 and as soon as I tried to put more than that, it violated the binary, the size of it and actually truncated and lost data. Now if I'm running this program and I've explicitly said yeah, you go ahead and just do this, I now have a serious logic error going on. I think I've stuck 400 in this and I've actually only put 144 in there. And again, go back and watch Casting Part 1 and you'll see why the binary made that happen. Notice here how easy it is to cast. OK, I'm going to break out of here. We'll talk about debugging later and this will make even more sense. Very easy to cast this and say you know what? Just treat that like a byte, convert that into a byte and load that into Y and that's the syntax. You just put what you want to convert to in parentheses in front of the data type and it will convert it. But again, you're basically saying look, shut up. I know what I'm doing. When you do that, make sure you know what you're doing because the compiler's going to shut up and let you shoot yourself in the foot if that's what you really would like to do. I hope this really helps understand casting and in a different video we're going to talk about converting, which is a very similar concept but it's different because of the value and reference data types. We'll talk about that in a later video called Converting. But this has been casting.

Tutorial Information

Course: Introduction to Microsoft C# 2008
Author: Mark Long
SKU: 34046
ISBN: 1-935320-78-5
Release Date: 2009-10-09
Duration: 7 hrs / 76 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available