Locked lesson.
About this lesson
We explain the concept of Null, discuss which fields can have a null value, and introduce Nullables.
Exercise files
Download this lesson’s related exercise files.
9 - Null Nullables.docx60.8 KB 9 - Null Nullables SOLUTION.docx
57.8 KB
Quick reference
Null / Nullables
Null is basically just nothing.
When to use
Use it when you need to represent nothing.
Instructions
string myName = null;
Nullables allow you to convert an integer to a null.
Nullable<int> myInt = null;
Hints & tips
- Strings can be null
- Integers cannot be null
- string myName = null;
- Nullable<int> myInt = null;
- 00:03 Okay, in this video, I want to talk about one more data type,
- 00:07 sort of a data type and that's null and nullable.
- 00:10 And null is the sort of a weird thing you'll come across now and then.
- 00:14 Probably more advanced coding you'll look at it more often.
- 00:18 But null is basically just nothing, right?
- 00:20 Some programming language is listed as zero, some is just nothing at all.
- 00:25 In C-Sharp it's basically just nothing at all.
- 00:27 And it's kind of weird some variables can be null and some can't.
- 00:31 So strings for instance can be null, integers can't, except sometimes they can.
- 00:36 So we're going to talk about that in this video.
- 00:37 So let's just start out creating a variable, and let's call it a string, and
- 00:42 we're going to call it myName.
- 00:43 And let's set that equal to John.
- 00:47 Now we can then reassign this or we could have just instead of calling John,
- 00:52 we just could have assigned this as null.
- 00:54 So let's just try that.
- 00:56 So let's come down here and let's concatenate on myName.
- 00:59 We go ahead and save this and run it and see what happens.
- 01:03 So go up here and run this program.
- 01:05 When we do, we see Hello and then nothing.
- 01:09 Now remember down here, we kind of catenated on my name.
- 01:13 You would think it should say something, but we define my name to be null,
- 01:17 which has nothing so it's returning, right?
- 01:20 So that works just fine for strings.
- 01:22 And you might be asking, why would I have a variable that has nothing in it?
- 01:26 Why would I need to define that?
- 01:27 We don't really care about that right now.
- 01:29 Right now we just want to sort of wrap our brains around the concept of null, right?
- 01:33 So like I said, you can have null strings, you can't have null integer.
- 01:37 So we could try an integer and we could call it myInt, and set that equal to null.
- 01:44 And then down here when we could try and print out myInt.
- 01:47 If we save this and run it, boom, right away, we get an error.
- 01:51 So we could try to run in any way and you notice it still has nothing listed, but
- 01:56 we get all kinds of errors.
- 01:57 And if we come down here and click on this little red circle here, we could see,
- 02:02 you cannot convert a null to an integer because it's a non-nullable value type.
- 02:08 So basically value types can't be null, except sometimes they can.
- 02:12 So if you do need an integer to be null,
- 02:15 you can sort of kind of convert it using a nullable.
- 02:19 And a nullable, we just call nullable and then we pass in the type we want.
- 02:25 So, in this case, we want to change int to a nullable and
- 02:30 then we can just say myInt and we can set that equal to null, all right?
- 02:36 So this stays the same, myInt.
- 02:39 Now if we run this, no errors pop up and
- 02:43 again we get Hello, nothing because now our integer is null.
- 02:49 So again, try not to read too much into this.
- 02:51 This is a more advanced topic and I didn't really want to get into it.
- 02:54 But we're talking about data types and it's sort of a data type, so
- 02:57 I just wanted to at least mention it.
- 02:59 So if you run across in the future you see null,
- 03:02 just think yourself null is nothing and just sort of leave it at that.
- 03:06 As you get further into your coding careers and learn more advanced topics,
- 03:10 you'll come to find reasons to use null in different ways.
- 03:12 But for now, just remember null means nothing.
- 03:15 Strings can be null, integers, decimals, floats, things like that not so much.
- 03:20 Unless you create a nullable and specifically define it, then it can be,
- 03:25 but otherwise, null is nothing, and that's all there is to it.
- 03:30 So, that's it for this video.
- 03:31 In the next video, we're going to start to look at string methods.
Lesson notes are only available for subscribers.