C# Essentials / Data Types
Subtitles of the Movie
Now, in this video I want to start off as we dig into the C# language and right off the get go here, let's talk about data types and first of all, if you're totally new to programming, you're asking yourself what exactly is this guy talking about? A data type is really a storage bin in memory for any data that you need to keep up with. Now, we know those as variables. As you write your program, inevitably you're going to need to take a piece of data, stick it in memory somewhere and then go recall it from memory a little later to help you control the flow of your program or whatever. Variables are a normal, everyday occurrence in programming. We don't even think about them anymore. We just kind of use them. As it happens, in C# there are two different kinds of data types and these two different kinds can make a huge impact on how your program behaves. So first of all, the first kind that we have is a value data type and by the way, let me just say this right quick. These data types are really just actually aliases for the data types that are defined by the common type system of the .NET Framework and all the data types have been kind of standardized across the .NET languages. But anyway, you can read about that in some technical documentation. If you're having trouble sleeping one night, get you some good documentation on data types. That'll take care of the problem. So anyway, this is going to be a very simple overview so you can understand it better. So a value data type, the first of the two kinds, this is where the data is actually stored in the variable. Think of it that way. If I declare X as an integer and I set X equal to three, then the three is actually stored in the X if you will. And I'm going to draw you a simple, little diagram or whatever in just a couple of seconds here to help you understand this. So the first kind of data type that we have is a value data type. The data is actually stored in the variable location in the memory location. The second one is a reference data type and this is where the variable that you create points to a storage location in memory and that's, there's a huge difference; a subtle but huge difference. And so what I want to do now is draw you a little picture of what I mean by these differences and now you're going to notice real quickly here that I'm an artist in my spare time, right? So just excuse the look of this but I want to help you understand. The first one, let's look at value type, OK? With a value type, we go into our program and we create an integer variable called X and we set X equal to four. Now, we created the variable called X. We told the compiler and we told our computer that this was going to be an integer data type and we wanted to store four in it so it went off into memory and it got a memory location and it named it X and it put the value four in there. Then we created a value, a variable called Y and then we just set X equal to Y but when we created the variable Y, it created in memory another location called Y. When we set it equal to X, it put a four in there. Makes sense. Then we come back later and we set Y equal to six and what happens here? Well, the four goes away. It now stores a six and X and Y are no longer equal. Does that make sense? I mean that's, you know, no big deal. No big deal they're not equal anymore because there were two separate locations and again, notice, the actual value is stored in the variable itself. So two separate variables, two different storage locations and we can change them without affecting the other, even though we copied the values of the variables and we copied the variables to start with. So now let's talk about a reference type and how is it different. The reference type, what we're going to do now is we declare X as an object type and we'll talk about those a little bit later and so now instead of X pointing to its value in memory, X now points to a location or X is a pointer that points to a location in memory and we set X equal to four so there's X and it's equal to four. Then we create Y off of the same object that we created X off of so that means that Y is pointing to the same place in memory. It's pointing to that same class. I don't want to go too deep here but anyways, it's pointing to the same place so now if we change X to six, guess what happens? Ta da! Y is the same thing and if we change Y to eight, then they're both going to be eight. So that's the main difference. A value type stores its data directly more or less in the variable, if you want to think of it that way and a reference type stores the data in a pointer that points to a place in memory and when we talk about object-oriented programming a little bit later on in the course and we talk about how objects and classes work, this'll make even more sense. But for now, this is a very simple explanation. It'll help you understand the difference now. A lot of times you will want to use a value type as opposed to a reference type but that's just one of kind of the weird things about programming. This'll just kind of happen. You will seldom thing I think I'll use a reference type here as opposed to a value. Now, in a more advanced world of programming later on you will think that way, but for now, you're simply going to think what kind of data do I need to store and you'll stick it in there and C# will take care of this for you. So anyway, there's the two types of data types that we can use to store our own data in our own variables; value and reference and I just kind of wanted to get you going on the difference between those two.
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 