C# Programming – Season 1 Console Apps – Episode 4 Variables And Input

C# Programming – Season 1 Console Apps – Episode 4 Variables And Input


Hello, I’m Soogbad. Now that you know the basics of the basics of the basics of C# and console apps we can get into more complicated stuff – the basics of the basics of C# and console apps. If you don’t know that go watch my other videos now. So before we get to variables and input let’s talk a little bit about errors. There are two types of errors – compilation errors and runtime errors. Runtime errors occur during runtime (when the application is running). There’s no way for the computer to know these errors are going to happen cause they depend on what you did in the application after you ran it. If it doesn’t depend on that then the computer can know these errors are 100% going to happen and these are called compilation errors Because of that the computer won’t let you run the application In the case of runtime errors you will be
able to run the application but if the error occurs it will close it and show the error. To see what error it is with the compilation errors you can hover with your mouse over where the error is Let’s demonstrate. Let’s say I forgot to put a semicolon after this sentence. Errors like that are called syntax errors Now there’s a red zigzag line underneath there. If I hover over it it will display the message (expected a semicolon). Now let’s try to run the application As you can see it didn’t let me and it opened the error list Wait a second now it opened. In here you can also view all the errors If you’re not sure what an error means google it Ok so now that we went through that we can continue to more advanced stuff – variables Variables are as I said things you can store data in them and Just put that semicolon over here ok. Variables have a type, a name and a value Name is to access them later with that name. Value is the value Type means the type of data they store (types like text, whole numbers, non whole numbers and so on). A variable can always store one type of data If you created a variable of type text you
can never assign that variable a number value For that you’ll need a new variable or to
convert the number to text So let’s get to the code. To create a variable you do this: type and then name and then equals and then value. Now it just marked some things in here but it’s not supposed to be the actual code, yeah? Or you can just end the statement before the equal sign without assigning a value but why that’d just be dumb cause why creating a variable and not using it So for now we’ll focus on variables that their type is text This type in C# is actually called a string. Basically if you want to say text you say string. So the syntax is like this: string and then its name let’s just call him text and then
equals and then the value which is going to be in this case Hello. Now I just named the string text because currently it doesn’t have any purpose but when naming variables always name them what they are used for Let’s say I create a variable to store input from the user then I’ll name it input and
not just text or str Now let’s change the Console.WriteLine to write the string we made This is completely useless because we can just write directly Hi without saving it into a string but for the purpose of learning let’s do that Now let’s run the program. Oh wait no. See? This is an error because that thing doesn’t exist Yes now let’s run the program and as you can see Hello and then Bye World like normal Now let’s talk a little bit about operators
of strings If you have two strings and then a plus sign in the middle then it can count as one string which is the two strings connected. Example: Let’s go and do text plus World Now let’s run it. Good you see Ok now for input. To get what the user writes we use Console.ReadLine That function lets the user write stuff and
waits until the user presses enter and then it returns the input to the code in a form
of string So let’s improve our code. Let’s Console.WriteLine Enter Input and then this string will be equal to Console… Wait I actually also have a shortcut for that. And then we’ll rename the string to input And we’ll Console.WriteLine Your Input Was plus input Oh no wait I pressed something just a second ok Now let’s run it ok? So as you can see it writes Enter Input and now it’s waiting for me to enter input Let’s write Poopy Poop and then when I press enter it writes Your Input Was and then my input again. Now here still and the Bye World I forgot that let’s actually delete that a second ok. Now here the variables are still not necessary you can just do this
But we’ll skip??? them for the purpose of learning. Now go try in your computer to make a program that will ask you for your name and then say helloOk now in case you haven’t succeeded this is the solution but you really should manage to do this because it’s exactly the same thing we did here but with different text So let’s change Enter Input to Enter Your
Name and then let’s change the variable name from input to name because that’s its purpose now Let’s change Your Input Was to Hello and then name and then let’s also add an exclamation mark. Ok so let’s just check it’s really working so let’s start the application. Enter Your Name Soogbad and Hello, Soogbad! This is it for this episode be sure to watch
the next one which will appear on screen right now and subscribe so you can watch the whole series!

Leave a Reply

Your email address will not be published. Required fields are marked *