Locked lesson.
About this lesson
We introduce many of the Data Types you can use: Strings, Char, Int, float, double, decimal, and boolean.
Exercise files
Download this lesson’s related exercise files.
6 - Data Types.docx60.7 KB 6 - Data Types SOLUTION.docx
57.6 KB
Quick reference
Data Types: Strings, Char, Int, float, double, decimal, boolean
Variables hold data types - which are simply types of data.
When to use
Any time you need to chategorize data, you'll use a data type.
Instructions
char myChar = "J";
string myString "John Elder";
int myInt = 41;
float myFloat = 19.95f;
double myDouble = 21.14d;
decimal myDecimal = 99.98m;
Boolean myBool = true;
Hints & tips
- The main data types are char, string, int, float, double, decimal, and Boolean
- There are more data types, but these are the main ones you'll use most often.
- 00:02 In the last video, we talked about variables.
- 00:05 In this video, I want to talk about data types.
- 00:07 And like I said in the last video,
- 00:09 we could put many different things into variables.
- 00:11 And the things that we're going to be putting into variables mostly
- 00:15 are data types, types of data.
- 00:16 And in this video, we're going to look at the main data types that you're going to
- 00:19 come across more often.
- 00:20 There are other ones besides this and
- 00:22 there are other things that you can put into variables besides these things.
- 00:26 But these are the main data types we're going to look at.
- 00:28 And you could see in the last video, we talked about strings.
- 00:30 I've got a little example that we looked at in the last video here.
- 00:33 We also have char, int, float, double, decimal, and Boolean.
- 00:38 So char is very similar to string, only it's just one char,
- 00:43 one character, like J, that would be a char.
- 00:47 Int, float, double, and decimal are all numbers we'll talk about.
- 00:50 And Boolean is true or false, and we'll talk about that too.
- 00:54 So let's start out with char.
- 00:55 I'm just going to do a comment for this,
- 00:56 we're not going to actually run this because it's not that interesting.
- 00:59 So we would just go char, I don't know, myChar equals J, right?
- 01:04 Just one character, and it acts the same way as string does.
- 01:09 It's just instead of, in this case, four characters in John,
- 01:12 we just have one character.
- 01:14 Now, in my life, I can't think of a single time I've ever used char.
- 01:18 If I was going to use a single character, I would probably still just name it
- 01:23 as a string because a single character is also still a string.
- 01:27 There's just very few times when you're going to need to designate something as
- 01:30 only one character.
- 01:31 But if you did, you could use char for that.
- 01:34 Now, let's talk about these numbers.
- 01:36 So integers are whole numbers, 5, 12, 106, versus a decimal,
- 01:41 $19.95, that's a decimal number, a floating number.
- 01:46 Integers are just flat out numbers.
- 01:48 So we could go int myNumber = 41.
- 01:55 We could come down here, we could do the same thing, we could put myNumber.
- 02:00 And if we run this guy, it says Hello 41, right?
- 02:03 It's a little confusing with this addition sign, are we trying to add something?
- 02:08 No, that's just how we concatenate things onto other things.
- 02:12 So we've got this string, we're adding this number to it, Hello 41.
- 02:16 So those are integers.
- 02:18 And next, let's talk about floats.
- 02:22 So let's go float myFloat = 19.95.
- 02:27 And you would think this would be good enough, but if we run this,
- 02:31 we're going to get an error.
- 02:32 So let's change this to myFloat, try and run this, we get build errors, why?
- 02:38 Because with floats, doubles, and decimals,
- 02:41 when you define it, you have to put either a d, an f, or an m.
- 02:45 So for floats, it's an f, so we just stick a little f next to it.
- 02:49 And this looks a little weird, I don't know why this is the case.
- 02:54 This isn't how most programming languages deal with these sorts of things.
- 02:58 But with C#, we have to designate a little f next to it.
- 03:01 So if we run this now, we get no errors whatsoever and it says Hello 19.95, right?
- 03:07 So kind of weird, but that's just how it goes with C#.
- 03:12 So let's comment this out and now let's do double.
- 03:15 So let's go double, and I'm just going to call this myDouble.
- 03:20 And again, we can go 21.95.
- 03:23 But again, this is a double, so we have to stick a d after it.
- 03:27 So down here, we can call myDouble.
- 03:31 And again, if we run this, we get Hello 21.95.
- 03:35 So finally, we have decimal.
- 03:38 So let's call decimal, and
- 03:40 I'm going to call this myDecimal because I'm very creative.
- 03:45 And here let's go 99.98.
- 03:46 So what do you think we're going to put into this?
- 03:52 Well, for double, we use d, so we can't use d.
- 03:56 So for some reason, it's m.
- 03:57 And the reason is decimals are often used for money, so
- 04:00 we stick an m at the end of this.
- 04:02 All right, so let's come down here in myDecimal, save this,
- 04:07 run it, run it one more time, and we get Hello 99.98.
- 04:12 So you may be asking yourself, hey, what's the difference between all of these?
- 04:19 Why would I use floats versus doubles versus decimals?
- 04:23 What's the difference? They're all just dealing with dot numbers,
- 04:27 21.95, 19.95, 99.98.
- 04:30 Well, that's a good question.
- 04:32 Like I said, decimals we often use for money.
- 04:35 So if you're dealing with money, I would use a decimal.
- 04:39 So floats versus doubles, the rule of thumb is to use double if you know
- 04:44 exactly what you're dealing with, 19.95, you know what that is.
- 04:49 If you're doing math and it's going to be a long decimal, 3.333333334, right,
- 04:53 3.14268969427, something like that, you're going to want to use float.
- 04:58 So float is better for using long trailing decimals,
- 05:01 we're not going to get into the why of all of this in this video.
- 05:05 Just a rule of thumb, if you know what you're dealing with, go with double,
- 05:09 if it's going to be very specific, double.
- 05:12 If you're not quite sure what it's going to be because you're going to be
- 05:16 doing math or something, go with float.
- 05:19 And int, of course, is just a whole number.
- 05:21 There are many circumstances where you're just dealing with whole numbers, so
- 05:24 you use int for that.
- 05:25 So finally, the very last one we want to deal with is Boolean.
- 05:28 And it's a funny sounding word.
- 05:31 So we can go Boolean myBool = true,
- 05:36 something like that.
- 05:39 Down here we can then go myBool, let's run this guy.
- 05:44 And when we do, we see Hello true.
- 05:46 Boolean can be either true or false.
- 05:50 Save this and run it, we see now it says Hello false.
- 05:55 And this is not a great example, it just says true or false on the screen, so what?
- 05:59 This is going to be more interesting and
- 06:01 more useful later on when we're doing if statements and
- 06:03 logic, and we need to determine whether a thing is true or false.
- 06:07 That's when you're going to use Booleans and then it becomes very useful.
- 06:10 But just for now, just realize that a Boolean can be either true or false, and
- 06:14 that's kind of all there is to it.
- 06:15 So those are data types, very quick crash course into data types.
- 06:19 Like I said, they go with variables, and they're pretty straightforward.
- 06:22 When you're using text, you're almost always going to use strings.
- 06:25 If you have a number that's a whole number, you'll use integer.
- 06:28 If you have a floating point, and take a float as it floats off into the distance,
- 06:33 there can be many numbers in your decimal, .19.968426486,
- 06:38 whatever, right, you use float.
- 06:40 Double is a decimal that's very specific, 19.95.
- 06:43 Decimal is money, 24.25, 24.95, on sale.
- 06:45 And Boolean is true or false.
- 06:47 So that's all for this video.
- 06:50 In the next video, we're going to look at date time.
Lesson notes are only available for subscribers.