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

C# Essentials / Casting pt. 1

Subtitles of the Movie

In this video I want to tackle the subject of casting and I want to try to explain it to you in a way that you'll really understand it because this can really confuse you and drive you nuts when you start to program. Now, in a different video in an earlier video entitled Data Types and then in another one called Built-In Data Types, we talked about how when we create our variables we have to tell C# what kind of data we're going to store there and really how much data and we tell it that by telling it the data type to use; byte, integer, that sort of thing. Well, sometimes in our programs we have to take data from an integer data type and put it into a byte or take data from a byte data type and put it into a variable that was created as an integer and there's two types of moving this data back and forth between these data types. There's implicit casting, which means that this is a widening conversion. We're taking data from a less-precise data type into a more-precise. This will be done automatically by C# or by the compiler because it knows if I'm taking a data that I can store say in a byte data type and if you remember, a byte we can store, let me think a minute here, we can store anything from zero to 255. So if I've got something stored in a byte data type and I try to then put that into an integer data type, well, the integer data type can store up to two billion so I'm not going to have any problems putting a number between zero and 255 into a data type that can store up to two billion, right? So there's no problem. It'll do it for me automatically. Here's where it gets interesting. An explicit cast is where we're doing a narrowing conversion. We've got data in an integer and we're trying to put it into a byte. Now, we could have a number of a billion and we're trying to put into a data type that'll only hold up to 255 so we're going to lose some data and so for us to explicitly program that kind of move between data types, we have to cast it. We're basically telling the compiler look, I know this could be a problem but I want you to do it anyway. Let me give you a picture here of what's going on in the background. When I move data between types, I can create some real interesting problems for myself and what we're really getting into is the binary means of storage for these things. Now, here's a good example. If I've got data stored in a byte and I'm trying to mov it to an integer, notice here I created a variable called D and it's an integer data type and I loaded it with the number 40. Or I instantiated it at 40. Now, we know that the integer data type uses four bytes or 32 bits of data and so this is what the number 40 looks like I in 32-bit binary notation. I'm going to show you a little trick here. I don't know if you've seen this before but I'm going to open the calculator. OK? So if I go to All Programs, Accessories and then open the calculator, I'm going to go to View and tell it to do scientific and I want to show you a little trick here. I don't know if you've seen this before. I hope so. Let me move this up here where you can see it. If I go to decimal and type 40 in and then click on bin, it shows me 1001000. So if I start from the right here, there's 1, we'll, it's not going to let me do that. But notice there's one space, two space, three space, four, five, six. This actually goes out to eight for a bit. I mean for a byte. So the eight bits. This is only using six bits. So notice 1001 is equal to the decimal number 40. I will just minimize the calculator for a moment. Now, if I set a byte equal to 40, not a problem because the number 40 can easily be represented inside those eight bits that make up a byte and so we can easily move this number right here from a byte into an integer. No problem. We're not going to lose any data because they both fit. I hope you see that. But now let's talk about going from an int to a byte which is narrowing. Now I've got a byte that is equal to 400. Well and I've got an integer that I'm trying, that I've got set at 400. I'm going to try to drop that into a byte and make my byte equal to 400. That's a better way of saying that. Now, my integer, if you'll notice, 400 takes nine bits. It takes the eight bits of a byte and then the first bit of the next byte and if I look at this on the calculator, let me bring that up or bring up my calculator and if I do 400, you'll see that it's taking up nine bits now and so from here to the left is in that next set of bytes. So now notice what happens. When I convert this from an integer down to a byte, what's going to happen here, the byte can't store from here over so it drops this. So when I move this from here to here, from an integer to a byte, I'm going to lose all of this stuff. So let's go back to our calculator. What we ended up with was 10010000, OK and let's convert that into English. So if I go to my binary, 1001and I'll look at that as decimal. It's 144. So when I read it here as D, it was worth 400. When I insert it into a byte, it's now worth 144 so I'm not going to get 400 out of here. I'm going to get 144. Does that make sense? It's a little confusing but that's the problem we get into. Now, in Part 2 of Casting I'll show you how to code this and that's coming up next.

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