C# Essentials / Built-in Value Types
Subtitles of the Movie
Now let's take a look at the built-in value types that are available to you in C Sharp that you're going to be using for your variables. So any time you declare a variable, which we'll talk about, show you how to do in a different video, you're going to basically be deciding what kind of box I want to store my variable data in. And I want you to think of it this way. You need to learn these different data types and the capacities that they have because in your attic, let's say you've got a box this large and you've to store some stuff. Well, so you start putting your stuff in and you're only storing this much stuff. Well, notice you've got all this area of the box that's not being used so you're kind of wasting storage space. OK? But now what happens, if, so you don't want to use this kind of box because you're storing a bunch of empty space and you're kind of wasting space. And in this instance you'd be wasting memory. But what if we've got this much stuff, right, it comes on up here and getting out of the lines but we have this much stuff to store. Notice our box is not big enough. We need a larger box to hold this and so when we choose our data types, we want to make sure that we choose a data type that will store the amount of data that we're actually trying to keep up with but we don't want to waste memory and this is not the big deal it used to be and a lot of people would take me out in a parking lot for saying this, but memory has become somewhat of a cheap commodity now. In the early days of computers it was very, very serious and always at the forefront of developer's minds but so anyway, let's just talk about these. You need to be aware of these and you need to learn these. The first type, if I create a variable and I give the type Byte, that means that I can store in that variable and notice these are all integers, meaning whole numbers. I'm going back to third or fourth grade math here. So whole numbers we can store from zero to 255 or if you're a real geek, 256 numbers. Next type is the SByte and notice with SByte we can store negative values in here; whole numbers still but negative 128 to positive 127. So if I need to store positive 128, then this data type is not large enough for me. The next one; in each one of these data types there's more and more memory, just as a rough idea for you, the Byte and SByte use one byte of memory to store the data. The Short uses two bytes and notice with the short we can store from negative 32,768 up to positive 32,767. Now ushort will go from zero all the way up to 65,535 and it uses two bytes of data. Now, it starts to get interesting. The standard Integer data type will store from negative 2,147,483,648 to position two billion blah, blah, blah, 647. OK? Blah, blah, blah being a technical digital term in the south. But anyway, with the Integer data type, notice you can store your paycheck for the next 36,000 years, you know, or anything else that you need to store with those kinds of numbers. You'll see most developers use the Integer data type just kind of out of habit. As information here, it uses four bytes of memory to store this. The uint data type doesn't store negative numbers but it will go from zero to four billion and you say the rest of the number there. Then it starts to get interesting. The long data type stores nine bazillions, I don't know what that is, from negative nine quadrillion bazillion, whatever that is, to position nine whatever that is. The ulong will store it all the way up to double basically either side of the long. It's 18 whatever that is. Forgive me for not knowing my numbers but too bad. Now, to store numbers with decimal places, keep in mind that the first eight that I showed you on that previous screen is all for integers, whole numbers. Now, to store decimals, the float will store up to seven digits which means as many digits as you want to to the right of the decimal place up to seven. The double will store 14 and the decimal will store up to 28. Now, the decimal type is the most accurate and so if you're trying to write an application or you're trying to manipulate money and dollars and get the most accurate decimal, obviously this is what you're going to use. The decimal type, by the way, uses 16 bytes of memory. You can store a single character in the char data type. Now that's a single Unicode character which really means it uses two bytes. There's 65,536 numbers represented there so there's 65,536 characters that you can represent and if you know the whole Unicode thing, that makes sense to you. If you don't, don't worry about it but you can use char to store a single character and then if you want to store a true or false value, we have Bool. Bool, if you're in the south it's Bewel. I don't know where in other parts of the world or country, but Bool is a takeoff kind of on Boolean. It's short for Boolean. You can store true or false in there so we can use that just to check for true or false values. Now, as you look at these different data types, you want to kind of get used to them. To start off it's generally safe to use a decimal data type for numbers and when you have decimals and you've got to maintain strict accuracy and a lot of programmers just use integer or int for their numbers but you need to learn these and get a little more discerning in your programming. But I wanted you to see the value data types. Now, coming up next in the next few videos that you'll see in the course here we'll talk about how to use these data types when we're declaring variables and using them. So that's your built-in value types. Just get familiar with them and learn how to use them appropriately.
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
United States 