Focus video player for keyboard shortcuts
Auto
- HD
- 720p
- 540p
- 360p
1.00x
cc
- 0.50x
- 0.75x
- 1.00x
- 1.25x
- 1.50x
- 1.75x
- 2.00x
We hope you enjoyed this lesson.
Cool lesson, huh? Share it with your friends
About this lesson
An introduction to the concept of Variables and how we will use them in C#
Exercise files
Download this lesson’s related exercise files.
Variables60.7 KB Variables - Solution
57.9 KB
Quick reference
Variables
A variable is like a box that holds things.
When to use
Use variables whenever you have any data that you need to store, in order to use later.
Instructions
To create a string variable:
string name = "John";
Hints & tips
- Variables hold data
- Variables can be changed or deleted
- string name = "John";
- 00:03 Okay, in this video I want to start to talk about variables.
- 00:07 And variables are probably the most important fundamental building block of
- 00:11 all coding, not just C# but any programming whatsoever,
- 00:15 variables are the most important basic element.
- 00:18 And a variable is basically just like a box or a bucket.
- 00:21 It's a thing you can put other things in.
- 00:24 And there are tons of different things that you could put into variables from
- 00:28 text to numbers to dates to other variables, mind blown.
- 00:32 To arrays and functions and all kinds of things you could stick into variables.
- 00:36 And we're going to get into the different types of things you can put into
- 00:39 variables later, specifically data types, but for
- 00:42 now I just sort of want to talk about the concept of a variable.
- 00:45 So I think of a box, what do you use a box for?
- 00:48 You put something in it, and you might need that, whatever that thing is now,
- 00:52 you can take it back out.
- 00:53 You might hold it in there for later.
- 00:55 In your program later on, you might reference it.
- 00:58 You might just stick it in there to sort of categorize it,
- 01:01 to sort of keep track of it.
- 01:02 And a variable's the same thing, it's just a place where you can put something to
- 01:07 keep track of to take out later.
- 01:08 Now you can change what's in the variable.
- 01:10 You can add different things.
- 01:12 You could add stuff to it, you could take stuff out of it.
- 01:14 It's just a very useful and fundamental programming thing.
- 01:17 So you're always going to use variables, you're always going to have them.
- 01:21 And they're super easy to use.
- 01:22 Now in a lot of programming languages, you could just use variables.
- 01:26 C# is a little bit more complicated, you actually have to define the variable,
- 01:29 explain exactly what you're going to use it for, and then use it.
- 01:33 So it's kind of a pain but that's just the C# thing, C's sort of like that.
- 01:37 Your different C programming languages are often like that.
- 01:40 Now you can do all of those things usually on one line.
- 01:43 You can define it, put something in it, and start using it all in one line.
- 01:47 Or you can break that into separate lines and I'll show you each one.
- 01:50 So we're going to get into the different types of variables in the next few videos.
- 01:53 In this video, we're just going to talk about strings and
- 01:56 strings are just strings of text like this right here, hello world.
- 02:00 That's a string, right?
- 02:01 It's just basically text of more than one character.
- 02:04 So to create a variable with string,
- 02:07 we just first define string and then we name it.
- 02:11 So let's call this first name, so here we've defined our variable, firstName.
- 02:18 So it exists and now we can use it, so we could use it by assigning something to it.
- 02:25 So we use an assignment operator, which is in this case, the equal to sign.
- 02:29 And here we could say, John.
- 02:33 Now notice at the end of each of these lines, I'm putting a semicolon.
- 02:36 That's totally normal with C#.
- 02:38 You're going to do that usually at the end of most lines of code that you write.
- 02:41 So here we have it, we've got a variable.
- 02:45 Now, down here we can use that variable.
- 02:46 Instead of this, we could say hello, and then concatenate it,
- 02:51 put a little plus sign, and then we could just put that variable, firstName.
- 02:58 So if we run this, we see it says hello John, very cool, and that's variables.
- 03:04 So this is a very basic example of defining and assigning a variable and
- 03:08 then using that variable, so, very cool.
- 03:11 Like I said we could do this in more than one line, like this.
- 03:15 We could do the same thing.
- 03:16 Let me comment this out and two slashes, that is a comment, two forward slashes.
- 03:21 That's a comment, so you can see it sort of greened it out.
- 03:24 We could do the same thing.
- 03:25 We could go string firstName equals John,
- 03:30 and now we've done it all in one line.
- 03:34 So if we run this, We get the same exact thing, Hello, John.
- 03:40 So sometimes, it just sort of makes sense to use two lines.
- 03:45 Most of the time, you'll just use one line and just sort of knock it out like that.
- 03:49 It really doesn't matter which.
- 03:50 A lot of times if you have a complicated program,
- 03:53 you might want to define all the variables at the beginning of the program, and
- 03:56 then later on you can assign things to them as you need it.
- 03:59 In that case, you would do it this way,
- 04:01 you would just define all of your variables, and then later on in
- 04:04 the program you would assign things to those variables as you want.
- 04:07 Now we can change things, like I said, a variable doesn't have to stay the same.
- 04:11 We can also then go firstName = Bob;.
- 04:16 Well, now if we run this, what do you think is going to happen down here?
- 04:21 Well, let's go ahead and see.
- 04:25 It says Hello Bob.
- 04:26 Why is that?
- 04:27 Well, we assigned John to firstName, how come it says Bob?
- 04:30 Well, this is a matter of program flow,
- 04:32 program flow starts at the top of the programming, works its way down.
- 04:35 So C# sees this, says, firstName is John, but then it comes to this line,
- 04:39 it says, wait, nope, it's Bob.
- 04:41 So it replaces what's in there with Bob, and then it prints it out, Bob.
- 04:46 So get rid of that, like I said we're going to use variables all the time, and
- 04:49 we're going to talk about them in much greater depth in the next few videos.
- 04:53 Like I said, there's all kinds of things you could put into variables.
- 04:55 In this place we've just put a string of text, but there are numbers, different
- 04:59 types of numbers, floats and integers and all kinds of different things.
- 05:03 There are concepts you could put in like true and false.
- 05:05 We'll talk about that later on.
- 05:07 You could assign arrays, different hashes, and dictionaries, and lists and
- 05:11 all kinds of things, and we're going to get into all that as we go forward.
- 05:14 But in this video I just want you to wrap your brain around the concept
- 05:17 of a variable.
- 05:18 It's like a bucket, it's a box, you can put stuff in it, you can take stuff out,
- 05:22 you can change it, update it, add stuff to it, and
- 05:24 we'll talk about all those things going forward.
- 05:26 And that's all there is to it, so that's variables.
- 05:29 In the next video we'll start to look at data types.
Lesson notes are only available for subscribers.