Locked lesson.
About this lesson
In this lesson, we learn how to capture User Input, assign it to a variable, and Output it to the screen.
Exercise files
Download this lesson’s related exercise files.
21 - User Input Output.docx61.2 KB 21 - User Input Output SOLUTION.docx
58.4 KB
Quick reference
User Input / Output
Gettting user input is very easy with C#.
When to use
Use this whenever you need to get user input in your program.
Instructions
Console.WriteLine("What Is Your Name?");
string name = Console.ReadLine();
Console.WriteLine("Hello " + name);
Hints & tips
- Console.ReadLine();
- 00:04 Okay, in this video, I want to look at user input and output.
- 00:07 Up until now, we've just been doing things and
- 00:09 outputting them into the terminal, right?
- 00:12 So that's interesting and that's good and that's nice to learn.
- 00:15 But what we really want is the ability for
- 00:17 our users to be able to interact with a program, to type stuff in,
- 00:20 and have the program do stuff based on what they're typing in.
- 00:23 And it's just a very basic sort of thing, but it's taken us a while,
- 00:26 almost 20 videos to get there.
- 00:28 So that's what we're going to look at in this video.
- 00:30 So we're going to start out here, let's say, What Is Your Name?
- 00:35 And if we save this and run it, we know exactly what's going to happen.
- 00:38 It's just going to print that line out to the screen, What Is Your Name?
- 00:41 Well, what if we want to actually answer and type in our name?
- 00:44 How do we do that?
- 00:45 Well, we can come down here.
- 00:47 And up until now, we've been using Console.WriteLine.
- 00:50 We can also use Console.ReadLine, right?
- 00:56 And this will allow a prompt where you can type something in.
- 00:59 So if we go ahead and save this and run it, We see What Is Your Name?
- 01:05 And now we have this blinking cursor and
- 01:06 the program is just sitting there waiting for us.
- 01:08 So we can type anything we want, hit Enter, and then the program ends.
- 01:12 So okay, that's cool, but that's not really what we want.
- 01:15 We want to be able to do something based on whatever was typed in.
- 01:18 How do we do that?
- 01:19 Well, what we're doing here is we're reading the line, but
- 01:22 we're not doing anything with it.
- 01:24 So if we want to actually do something with it,
- 01:26 we have to assign it to something, sticking in a variable, and
- 01:29 then we can do something with that variable.
- 01:31 So let's create a variable called string and let's call it name, and
- 01:35 set that equal to this whole thing.
- 01:37 Mind blown, we can take this line here and assign it to a variable.
- 01:41 Yes, you can.
- 01:42 So we can do that.
- 01:43 If we save this and
- 01:44 run it, we're going to get the same exact thing as we did just a second ago.
- 01:48 Nothing actually happens.
- 01:49 But, believe me, it is assigning it and sticking it in that variable.
- 01:54 We now just have to do something with that variable.
- 01:55 So we can come up here and we can create another one of these guys,
- 02:01 and we can do something now.
- 02:04 So let's use our string interpolation and
- 02:07 let's say, Hello, and then put in name.
- 02:11 And then, there we go.
- 02:12 So let's go ahead and save this, run this one more time, What Is Your Name?
- 02:18 John, hit Enter.
- 02:19 Hello John, prints it out right there, very cool.
- 02:22 And like anything else with the write line, we can continue on and
- 02:26 do anything we want.
- 02:28 Thanks for playing.
- 02:31 I don't know, we save this and run it.
- 02:36 What's Your Name?
- 02:37 John, Hello John, thanks for playing.
- 02:38 That's not a very exciting game, but there we go.
- 02:41 So just that easy to get user input and then output it again later on.
- 02:47 When we assign this to this variable name, it's in there.
- 02:50 We can do anything we want with it.
- 02:52 It continues to be in that variable, to be assigned to that variable,
- 02:55 as long as this program is running.
- 02:57 If you wanted to have more permanence, you would have to save it to a database or
- 03:01 some sort of file or something.
- 03:02 But while our program is running, we know that name is whatever we typed in at
- 03:07 the prompt, and that's all there is to it.
- 03:09 So that's all for this video.
- 03:10 In the next video,
- 03:11 we'll use what we just learned to create a fun little matlib program.
Lesson notes are only available for subscribers.