Build a C# .NET Application in 60 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to c-sharp programming for beginners in this video I'm going to give you an introduction to the c-sharp programming language and the dotnet framework this is actually the first c-sharp video on my channel I've been working with it recently and I really want to start working in videos and projects using C sharp and dotnet so this video is definitely going to be geared towards beginners if you've never used C sharp before that's fine you're in the right place if you have any experience at all with object-oriented programming on any level then you should be able to keep up with with this video just fine and then if you've never actually typed a line of code in your life you'll still take stuff away from this but of course I would suggest that you you look at other resources as well alright so without further ado let's go ahead and get into it this video is sponsored by dev Mountain if you're interested in learning web development iOS or UX design dev Mountain is a 12-week design and development boot camp intended to get you a full-time job in the industry to learn more visit dev mountain comm or click the link in the description below alright so initially I was going to do a crash course and go through things like all the different data types and how to create functions and all that really not building an actual application similar to Derrick banners tutorial on YouTube which is awesome and I would definitely suggest checking that out as well but instead I chose to start from scratch and we're going to install visual studio set up a project and we're going to build a really simple windows console application okay so many of these of videos like this they'll do like a simple hello world will just print text out onto the screen and I want to go more in depth than that so we're going to build a console program that will generate a random number and then let us guess it and we're going to add logic we're going to make sure that the the input the user types is actually a number we're going to be creating custom functions we'll look at code organization loops conditionals variables all that stuff alright so it is kind of like a crash course but structured in the way that we're actually building a simple application alright so what I'm going to do before we actually jump in and install Visual Studio and get going I just want to talk about some of the main points of c-sharp and dotnet justjust for people that are completely new to it so what exactly is c-sharp it's a general-purpose object-oriented programming language now I know a lot of you guys are web developers and may be new to languages like C sharp and Java but even if you know PHP or JavaScript Python any of these other languages that worked with that work with objects and classes you're going to see a lot of similarities and you should be able to pick this stuff up really quick all right once you get to a certain point it's really easy to learn new languages because they all have the same general fundamentals all right so c-sharp was created by Microsoft and it's one of the languages of the dotnet framework which I'll talk about in a second now as far as difficulty c-sharp is relatively easy to learn in my opinion I think it's easier to learn than something like Java but some people do disagree with that I also think it's a great first language it really encompasses all the things that you need to know for you know learning common principles in programming and it really prepares you to work with other languages as well so if you want to learn how to create Windows applications c-sharp is a great choice it can be used to create applications on other platforms as well but you may need to use some extra tools such as mono which you can check out I think it's the mono project net Java is probably a better choice for for building applications on other platforms but c-sharp is definitely the choice for Windows alright so let's talk a little bit about dotnet and what what that is okay because it really confuses some people between C sharp and dotnet it's kind of a blanket term but generally when you talk about dotnet you're talking about the framework okay it's technically a framework in a runtime that runs primarily on Windows like I said there's other tools you can use like mono which allow you to create applications for other platforms like Linux and and so on but for the most part you're going to be dealing with windows with c-sharp now c-sharp is is not technically it doesn't technically require the dotnet framework in simple terms if you want to do anything meaningful though you will need it you can kind of think of it as c-sharp as the airplane and then dotnet is the runway you can technically use the airplane without the runway but you're most likely going to use the runway in all situations to get your plane up off the ground all right unless you just want to blindly drive a plane into the woods so c-sharp is not the only dotnet language as a bunch including visual basicnet net c++ / CLI which is a certain version of C++ it includes extensions for CLR objects and I'll go I'll go over CLR in a second there's also f-sharp this kobold is there's a lot of them and you can look those up just google dotnet languages and you'll find them all all right in dotnet is made up generally made up of two main components which is the CLR and the class library also called F CL or framework class library and that gives us a ton of reusable classes interfaces value types and much more all right so it's basically the foundation of the dotnet framework so what is CLR and don't worry about it this stuff confuses you this isn't stuff that you really need to know and master in order to start learning c-sharp all right but I do want to just go over it because I don't think it would be fair for me not to say anything about it and jump right into the syntax so CLR stands for common language runtime and this is the virtual machine component of the.net framework okay it manages the execution of programs it's similar to the Java runtime it provides an environment for our applications to run alright so it has a process called JIT JIT are just in time and it converts your compiled code into machine instructions that your computer understands okay it also provides services like memory management type safety garbage collection if you're a web developer then you're going to be new to this kind of stuff compiling your software and I think that you're going to find it really awesome when you start messing with system resources and you get use some of the stuff that's in the class library all right so you can interact with you know the hardware on your machine which is really cool and it's much different than working with just you know web development on a server so CLR also lets use components that were built in other languages which is really nice all dotnet programs are executed by the CLR okay whether it's you know f-sharp or whatever else it may be now don't like I said don't worry if this stuff confuses you do it you don't need to master it to write c-sharp code especially for what we're going to be doing in this video I just wanted you to be somewhat familiar with these terms and what goes on behind the scenes okay so this this is the type of stuff that books are really useful for they drill that into your head but what I want to do in this video is just get you started with the syntax and structure of building a c-sharp program alright so this is I believe this is the last slide we're going to use we're going to need a tool to write our code obviously and also compile it and for that we're going to use an IDE which stands for integrated development environment as I said earlier I know most of you guys are web developers and you probably know what an IDE is even though you don't really need it for most web development okay so for you know when we're coding JavaScript or PHP something like that we can use our standard code editors like sublime atom and even Visual Studio code which kind of has a feel like Visual Studio just without the extra tools alright so an IDE is kind of like a text editor on steroids it has compiling and build tools extensive debugging and tell a sense and much more all right there's a few different versions of Visual Studio we're going to use the community version which is absolutely free the professional and the Enterprise Edition is not free it's actually very very expensive if you're working for a corporation building apps and stuff you would be--would promotes likely use that alright guys so like I said I don't want to spend too much time on this stuff just talking about C sharp and dotnet I want to jump right in and get our feet wet so let's go ahead and we'll install Visual Studio and then we'll get started on our simple application alright guys so we're going to start on our application first thing we're going to do is install Visual Studio Digital Studio is the IDE that we're going to use to create C sharp and dotnet applications so this is Visual Studio calm and like I said before there's a few different versions so we're going to go into downloads and you see that first we have the Community Edition here and that's what we'll be using it's it's fully featured it's completely free and it's for individual developers so then you have the professional version which offers extra subscription benefits for small teams and then Enterprise is an end-to-end solution for you know larger scale corporations and things like that and then Visual Studio code which is just the text editor which you've probably seen me use quite a few times it's similar to something like sublime text or atom or webstorm something like that so we're going to grab the Community Edition right here so let's click the free download button just close that out and that should download okay let's go ahead and open that up okay so so we're going to get this pop-up here we're just going to say continue and remember this is a large program so the in some of the installation may take quite a bit of time so you may see me kind of like you know pause if something is taking too long so we don't sit here and awkward silence all right so now you're going to get a screen like this and this is basically allowing you to set up different types of environments so up here we have windows we have Universal Windows platform development using C sharp or VB JavaScript and then over here we have dotnet desktop development that's what we want to choose alright but just to look at some of the other ones here we have desktop development with C++ we have web and cloud development so asp.net Python if you're deploying to Azure nodejs go down here you have mobile and gaming we've xamarin all types of gaming development C++ and then we have other tools that Linux development for C++ so there's a lot that you can do and it can get very complicated but try not to get overwhelmed with stuff like this because we're only using a certain part of of this IDE so let's go ahead and click make sure that that's checked and we'll click install all right and this could take a little bit depending on your system and how powerful it is so we're going to go ahead and pause this and when this is done I'll be back alright guys so the installation is complete and unfortunately I'm getting a reboot required message here I'm not sure why I don't think I've ever had this before so if you have it just go ahead and reboot I'm going to have to restart so I will be back after that all right guys so I am back I just reread it or real aughh din whatever and I started up Visual Studio we get a screen like this so this is going to ask if you want to sign in using your azure credits we're going to go ahead and skip this and say not now maybe later and you can choose a theme I prefer to like dark code editors over the light so I'm going to choose dark but of course feel free to use one of the other ones and then we're going to say start Visual Studio all right and that should go ahead and open up it's opening up on a different screen let me just pull it down maximize this so this is the start page or the Start screen and try not to let this overwhelm you there is a ton of stuff up here a ton of options there's these icons you have these different panes on the right and left so don't don't get overwhelmed by that stuff we are not going to need most of it so this is just the start page gives us some some help some documentation we're going to go ahead and just close that up and then what we want to do is go to new project and then you're going to get a bunch of different options here because we can build desktop applications we can build console applications which is what we want so make sure that you have visual c-sharp highlighted over here and then we're going to go and we're going to build a console app with the.net framework okay and down here we can name our application so I'm just going to call this I'm going to call it number guesser alright and then the default location is going to be in your mine is actually my Dropbox documents because that's set to my main documents but if yours isn't it'll just go right in your regular documents and then a folder called Visual Studio 2017 and then the folder called projects all right now I'm also going to uncheck this create directory for solution because I want to have everything in one directory I don't want it separate alright then we're going to just click OK and that's going to open up our project inside of the IDE alright so every c-sharp project you create is going to look like this by default and let me make this a lot bigger so you guys can see what's going on here so we start with one main file with one name one main class and it's called program and if you look over here in the solution Explorer and we look under program dot CS that's our actual program okay that's the actual this file that we're looking at here program dot CS all right and then this other stuff here right now we don't really have to worry about this it gives us some properties if we look at this assembly info dot CS file this is kind of kind of a configuration you have the the title the description the kata year all that kind of stuff and if we look in app config that's an XML config file and we're not going to really deal with that but just know that it is there okay and then let's go ahead and look at the main file so we'll start with a namespace a namespace is basically like a container for classes and functions so in the event that you have two classes called the same thing or two functions you can you can reference the namespace so you know exactly what's going on and then your program doesn't get confused so by default the namespace is going to be whatever you called the application but of course you could change it if you want now up here we have these using statements using system using system collections generic these are basically bringing in other namespaces with other functions and other things that we can use inside of this program alright or inside of this namespace so the only one we're actually going to need here is system so I'm going to get rid of these other here we wanted to let we we don't want to have code that is doing nothing for us just to keep things simple all right good because we're going to be using for instance the console class which comes from system so inside our namespace our number guests our namespace we have a class and the class is used to create objects from okay whenever you're working with object-oriented programming you're working with classes in this case it's called program by default and inside classes we can have functions and we can have variables also called methods and properties all right so whenever you hear the word method it basically just refers to a function inside of a class so let's add some comments here so this is the namespace this is going to be our main class and then this is going to be basically the entry point for our application okay so we'll say entry point method sorry about that and actually before we get into that let's talk about static and void so void is going to be the return type of this particular method or this function basically that means that there isn't going to be one there isn't it's not going to return anything now you can have functions and or methods you can have them return things if it's a string or an integer you would you would put that here but in this case we're not going to have any return value and then static is a little tough to explain to someone that's new to object-oriented programming but basically when we create a class we can instantiate objects from that and we can create multiple instances of that object using that class well when we use static we don't have instances we're referring to the class itself or the I'm sorry the function itself all right so that's what static means and then in here is where we can start our code so let's just say start here all right now I'm going to start by just outputting some text now like I said we're using this system namespace here which comes with a whole bunch of stuff we can use so there's actually something called console Jesus I can't type so console and you'll see we get this nice little intellisense drop-down that tells us of all different all different information about each kind of library or class or object so it says represents the standard input output and error streams for console applications now if I go console dot you'll see that we get another drop-down and these are all different functions and properties of this console so we have background color we have clear cursor left cursor right all these different things that what we want the method that we want is actually right line so if we go down here very bottom you'll see right line okay of course you can just type it in but you can also select it and then that is actually a function so you want to put your parentheses and then in here we want to we want to output a string we're just going to put some double double quotes in here and let's just say hello world now I'm going to save it with ctrl s and then if we want to run this program it's very simple all we have to do is do a ctrl f5 now Mike my console actually opens on another monitor and that's that's how I want it when I develop unfortunately I'd rather it open on this monitor just for this video but I don't know how to do that I've never had to do that so I'm just going to have to drag it down when I open it but anyways you can see that it's outputting the text hello world ok so if we look down here console.writeline is going to write that line now if we just did console right which is another function that we can use let's save that and run it and then if I drag that down you'll see it does it prints out hello world but the next line is going to be it's not going to be a next line it's going to be on that same line ok so that's the main difference between right line and right in most cases you're going to use right line now as far as variables go c-sharp is a typed language meaning that we need to define the types of data that's going to go into a variable for instance if we want a variable called name we can't just do name and set that equal to to use double quotes and set that equal to something here you'll see we have an error and if we look at that if I hover over it says the name name does not exist in the current context so what we need to do is we need to mark this as a string because that's what it is okay any to any string in between quotes it's going to have any basically any character you want is going to be a string now we have this green squiggly line which is basically kind of like a warning or notification or just like a heads-up and it's telling us that the variable name is a sign but its value is never used because we assigned it the word that we're not using it anywhere so it's kind of giving us a heads up saying do you really need this alright and if we go down here let's replace hello world with name and now that little squiggly line disappears because we're actually using it so let's go ahead and and save this and we'll do a control f5 when we bring this down and you'll see that it's outputting Brad Travis E press any key to continue if we hit any key it's going to just should just close up there we go and if we wanted to for instance add some text like say let's say hello so we could just put in double quotes and then we can concatenate with a plus sign just like we would in JavaScript I know a lot of you guys are JavaScript developers so you can also think of console write or write line as console.log okay same type of thing so if we save that and we run it with ctrl f5 and bring it down you see hello Brad Travis E alright and close that now if we wanted to create an integer let's say my age an int is what is the type that we would define for that sorry about that that dinging actually I'm going to put this on vibrate alright so we would say int and let's call this age and we'll set it to 35 and then down here let's actually get rid of the hello and then we'll say name and then we'll concatenate on to that and we'll say is space and then concatenate age all right so if we go ahead and run that then we get Brad travesty is 35 now there is actually a better way to do this and that would be to say console dot write line and then here we can use placeholders for our dynamic values for our variables so if we put some curly braces and let's say 0 I'm sorry these need to be in quotes so it's a zero and then we'll just do space is and then one and then we can add parameter here we'll put a comma and then we want to put the name and then another comma and the age all right so let's go ahead and actually let's comment this out and save it and run it and if I bring this down you can see Brad travesti is 35 so you can use these placeholders and then whatever you want to fill them with just put them in here in the correct order okay so for example if we had another another value here we would just put curly brace and then two okay so you just want to go from 0 up all right so these are actually not part of the application I just wanted to kind of explain some of the some of that stuff before we begin so for application when we first opened it up I wanted to have kind of a header with the name of the application the version and the author's name so what we'll do is set a couple variables here so let's say set I will say set apt VARs and let's say these are all going to be strings so string and then we'll say app name and we need to use double double quotes here and let's call this number guesser and then we'll do at version and we're going to set that to 1.0.0 and then let's do app author and we'll set that to I'm going to put my name but you can put yours find everything I do here is totally open source and you're free to do what you want with it so now we have the variables now let's just do a console console dot write line and we're going to use the those placeholders so we'll say zero this is going to be where the app name is and let's put a colon and then we'll say version 1 and we'll say bye and then 3 and then we just want to add in here app name app version and app author all right so let's see what that looks like we'll save it and run it ctrl f5 and getting an error here index 0 base must be greater or equal to 0 what do they do I put a 3 there that should be 2 all right so now if I bring this down we get number gets our version 1.0.0 by Brad Travis City so that's kind of the itty header I guess you could call it so now what I'm going to do is show you how we can actually change the text color on the console because I want this this application intro or header I want this to have a different color so what we're going to do is go right above where we did the right line and let's put a comment in here and let's say change text color and to do this we can say console dot and then if we look here these are all the different properties we want the foreground color you also have the background color if you want to change that but let's choose foreground color and then what we'll do is set that equal to and then we have to do console dot console dot and I'm sorry what is it console color dot and then you'll see all these different options so you can choose any of these we're going to we're going to go with green all right so let's save that and we'll try and run it and I'm going to bring this down now notice that the color has changed to green but it's not just for this line it's going to be for anything after that as well so what we have to do is we need to set it back after we're done with that color so after we write it out let me just I want to put a comment basically for every single line of code we write so you guys know exactly what's going on all right so let's just say right out app info and then down here we're going to say console color dot not console color we want to do console dot reset color all right and that's going to set it back to the default of white let's put a comment here preset text color so now if we run that let's bring this down and now you can see only this line here is green okay it goes back to white after that so now that we have our application info at the top we're going to proceed on now before we do the actual game where we need to guess a number I want to I want to ask us our name and we'll put the name in and it'll give us a greeting okay very simple so let's go down here and later on we're going to separate all this into separate functions so it's not you know all inside of this main function makes things a little cleaner so see how we greet user all right so we're going to just let's put a comment here we'll say ask user's name so console dot write line and let's say what I keep with the single quotes in here I want to use double what is your name all right and then they're going to answer that and we're going to put that input into a string okay so let's say string input okay it's just a variable called input and we're going to set it to console dot read line okay and that's going to put whatever the user enters is going to go into that variable so after that what we want to do is just write out basically hello and then their name and then we'll say let's play a game so let's do console dot write line and then in here we'll say hello put our placeholder and then we'll say let's play a game okay save that let's try it out bring this down and we get our application info here and then we get what is your name so I'm going to say Brad enter oops we need to write our second parameter here which is going to be whatever they put as input okay whatever they put into the console and it'll be put inside of input and we want to replace this placeholder with that input let's try again it's close that up control at five bring this down what is your name Brad hello Brad let's play a game good so that works good now we're going to start the game so first thing we're going to want to do is we're going to set our number okay so it's a set correct number we're going to it's going to be random but for now we're going to actually set it manually so let's create an int we'll say int correct number and we're going to set that equal to seven by default all right and ignore the squiggly lines here it's green that means that it's kind of just a heads up it's telling us that we haven't used that value anywhere now we're also going to set another variable for the guess so we'll say init init guest var let's set this to an it correct number so int and we'll call this guess and we're going to set it to zero by default all right then what we want to do is tell the user or ask the user to guess a number so it's a console dot write line and let's say guess a number between what's between one and ten okay these should actually be double quotes so let me do a few things I try to explain this the way that we want this to work is once we put in the number if it's not correct we want it to ask us again so we have to use a loop for this because we want it to want to basically say if the number doesn't match then we want to loop through and we want to ask again and again until the user actually gets it right okay and you can make it work differently if you want but that's how this this is going to work we just put a comment here so they ask user for number so we're going to need to use a loop so we'll say while we're going to say wild guess okay so guesses remember it's initially set to zero so wild guess is not equal to the correct number then we're going to do what's it whatever is in here all right let's put a comment here as well say while guess is not correct okay and then we're going to want to get the user's input so let's put that in a variable now by default it's going to be a string so we're going to set our variable to a string set it to console dot readline why is that what's going on cannot be declared in this scope because that name is used in an enclosing local scope oh because I used it up here right okay so this this later on is going to go into its own function so we can use input then but for now we want these to be different so let's change this to input name that makes a bit more sense to it's more descriptive and then we will just want to make sure we change it here as well and then down here we'll just keep that as input okay which is going to be a string because when we get the user's input it's a string now we're going to want to take that input and we're going to want to parse it into an integer and put it into the guest variable which is initialized up here is zero all right so let's do that let's say cast two int and put in guess put it into the guest variable so let's do guess which is already defined as zero and we're going to set that to input that the user enters but we need it to be an integer so for that we can use we can use a function called int 32 dot parse okay and that will parse it into an integer it'll put it into this event into this guess so now we have the user's guess so we need to match it to the correct number up here that is seven so let's do that so match guess two correct number so if all right we're actually going to say if guess is not equal to the correct number so if they don't answer seven then what's going to happen we're going to want to spit out a message looks let's spit out a message just like we did up here and I want to change the color too so let's copy this which is changing the color while putting the message and then resetting the color back later on this will put this into a function so that we can reuse it and it's not we're not repeating ourselves as much but we're going to change this to red because it's kind of an error message and then let's change the the actual message here get rid of all that and we're going to say wrong number please try again so it saved that ctrl f5 let's bring it down and put our name in gets a number between one and ten we'll say one which we know isn't correct wrong number please try again two and so on now if I put seven and enter it just ends because we're not doing anything after that so what we want to do is now go outside of this while loop right here because now guess is actually going to be equal to the correct number so that ends and if you just put the cursor before the curly brace you'll see where it ends so you can see the while loop ends right there so that's where we're going to go and we're just going to output success message okay so basically actually basically doing the same thing here I want to change the color to yellow so I'm just going to copy that and just replace that this needs to be changed this is going to tell user its the wrong number then down here we're going to say tell user it's the right number and I'm going to change it to yellow not red so for this we'll just say you are correct and then we'll reset the color so let's try that ctrl f5 okay let's give it a shot against the number one wrong number wrong number let's do 7 enter you are correct all right so we are getting there now we have a major issue in our code and that is if we go back here and it asked us for a number and I put in B and enter you'll see that it we have as our input string was not in a correct format so that's that's a big issue in our code so what we need to do is we need to check to make sure that the input the user puts in is actually a number and not a letter or a special character so we're going to have to go to where we capture that input which is right here okay so this is where we get the user's input for the number and we put it into input and then here is where we actually translate it or or or parse it into an integer so we need to go in between those two all right let's say make sure it's a number so to do that we can put an if statement here and we're going to use something called try parse all right so let's see we're over here so we're going to say if not int dot try parse and if we look at that it says converts the string representation of a number two it's 32-bit signed integer equivalent alright so it'll take a string of what's a seven and then turn it into the actual integer seven and then it says a return value indicates whether the conversion succeeded okay so it'll tell us if it actually if it succeeds so if it if it is in fact a number so let's do try parse and then we get we need to pass into here input okay which comes from the user input and then we're going to say out and then the guess variable here all right and then if it's not a number then we're going to just print a message so let's let's just copy this right here and put that in there and you can see we're repeating ourselves quite a bit with this that's why later on we'll put it into a specific function so we're going to keep it read and then we need to tell user it's not a number all right so we'll say sorry or now let's say please enter an actual number all right and then we just want to go to the bottom here and let's say keep going so we just need to say continue all right so let's try that okay what is your name guess a number that's wrong let's let's now put something that's not a number we'll say D please enter an actual number so as long as I don't enter a number it's going to say that let's put in four so it's wrong number please try again seven you are correct alright so now we're making sure that a user has to put in a number so everything's working but what I want to do is instead of having a fixed number like seven I want to generate a random number each time so let's actually just comment this out and then we want to create a new random object so to do that we can say let's see we can do random random equals new and then there's actually a class called random are an object that we can set that to so we have this variable now that we can use that has methods attached to it so we're going to set a correct number just like we did up above whoops not in it I want to say int correct number and we can now take that random object and we can call dot next okay which just generates a random number and we can pass in a parameter or two parameters here and say that we want it to be from 1 to 10 and that will give us a random number you can see how easy and how sleek that is let's just put that comment here alright so now let's try it out and now we don't know what what the actual answer is so it's like we're playing the actual game so let's just start at 1 wrong number and we'll just go up and it was 3 all right so the functionality is is just about done but what I want to do is make it so that when we're done we can play again because right now we'll just exit so to do that we're going to have to put everything inside of a while loop all right so we want to go under where we set the application title and all that we want to go under the greeting of the user we want to go right to right under where it says let's play a game and let's say while true and that'll be an ongoing loop ok it's always going to be true we want to wrap it around everything so we want to wrap it till right here oops okay and you see it pushes everything over which is really nice so now we have this wild true and then all of this stuff is inside of it okay and then it ends here so at the bottom here right under this reset color we're going to ask the user if they want to play again all right so we'll say ask to play again and we're going to just do console dot write line and we're going to put in our double quotes and we'll say play again and let's let's put in brackets here Y or n okay we want them to put in yet either wire n for yes or no all right and then let's get the answer so we'll say string and we'll just call this answer and we'll set it to the console dot read line now they might not know to make it an uppercase Y or n so we want to just change it to upper no matter what and we can use a simple function we can just chain on to this and we can say to upper and that will make it an uppercase alright so now we want to check to see if it was either a yes or a no so let's say if okay this is going to actually be an if-else okay so let's say if answer is equal to uppercase Y let's put this on a different line too and then if yes all we need to do here is do a continue okay and then it's else then we want to return outside of the loop actually you know what we don't want to do else let's not do an else let's do actually not yeah let's do an else but we want to do an else--if because we need to check for the answer if it's no so let's say else if and here let's say if answer is equal to N and then we're going to return all right so it'll only continue if they answer why so I think that's it let's go ahead and try that all right let me bring this down I guess a number just going to start from one and go up you are correct play again let's say yes guess a number five okay let's say no enter and there we go it end it's good so our application is done as far as functionality now we want to clean this up a little bit because we're repeating ourselves quite a bit also things like where we did the application intro I want to put that into its own function so let's go underneath the main function here for you hi like we put the cursor right here at the beginning of the curly brace and go down you'll see where it ends okay so that ends there so now let's create a function to get the app info so we'll say static void get app info actually I want to start this with a an upper case we're using Pascal case okay and then what we'll do is grab these variables we're also going to grab the foreground color the right line and the reset color and we're going to cut all of that and then in its place we're going to say get app info we can put a little comment here if we want I'll say run get app info function to get info I don't know just writing whatever and then down here we'll paste that functionality in so let's save it and make sure everything still works even though we added that function okay so it it set you can see it's putting out the info which is that's the part of the the program that where we put into that function everything else should work good and then I also want to put the greeting into its own function so we're we asked the name we get the input and we say hello let's play the game we're going to cut that stuff out as well and then let's say we'll call it greet user and we'll say ask for user's name and greet okay there's an error here because it doesn't know what that is yet so we need to go down and create that let's go right under the get the get app info function and we'll say static void greet user and let's go ahead and paste that in alright and we should probably put calm comments here as well we'll say get the will say get and display app info and then this one here will ask a user's name and greet okay so that looks a little cleaner now what we'll do is let's create a function to basically print to the screen with a color so we're doing this in a lot of different places if we start up at the top here we go down and let's see right here so this part where we're telling the user to use an actual number if they didn't enter a number so we'll grab the foreground color the right line and the reset color and we're going to cut that out and then let's go down here and let's say print color message and then we'll call this print actually we need to do static static void print color our print color message okay we'll paste that in now this is this function is actually going to take some parameters it's going to need the color and it's going to need the message because those are always going to be different so right here we need to define the arguments now the first one is going to be the color but it's actually going to be the console color so we need to define it as that so console color and then color alright and then for the message that's just going to be a regular string okay and then we're going to just replace this with color which comes in through the first argument and then replace this with the message like that so then we can go back up to where we took this from which is right here and let's say print I guess print error message and we can say print color print color message and then remember the first parameter has to be a console color so we'll say console dot NOC dot just console color dot and then whatever color we want in this case it's going to be red okay and then you can see it has a red squiggly line before we put that comma in because it takes in two parameters but we only have one okay you can see it gives us some pretty good descriptive information about what this takes in so console color red and then the message was what was it please use an actual number so now whenever we want to print out a color message we can just do this which is much cleaner so let's copy this actually we'll just copy that and then let's see where else we can plug this in so we're also doing it here when we check to see if it's the correct number if it's not then we're setting the color to red we're printing out this message and we're resetting the color so let's just paste this end and then we want it to be red so we'll leave that but then we want this message so we'll just paste that in and then we can get rid of these lines okay so it really tightens up the code and I guess we'll just say set error message or print error message I know these comments aren't the best but I'm just trying to try to be descriptive so let's see where else can we do this we can do it right here we're saying you are correct which and the color is yellow you are correct and then reset so let's go ahead and just replace all that copy this and replace that and this is going to be actually a success message so this is going to be a console color yellow and then we'll say correct you guessed it I think that's it so let's save it and make sure everything works okay what is your name number wrong number please try again let's put something else in it's not a number we'll put a character in good and now we'll keep guessing numbers 9 is correct do I want to play again we'll say yes guess a number correct let's say no and we exit good so we now have a working c-sharp application now if you want to get your actual file that your executable we can go to the Documents folder mine is actually my Dropbox and then if I go to visual studio 2017 and go to projects we have our number guesser folder this includes everything all the configs all the properties everything but if we go to the bin and then debug you'll see number guests or exe this is our application just make sure that it says type application and you can take this and you can give it to anybody I'm just going to copy it and put it right on my desktop and let's go ahead and run it and I'm just going to bring it down here and you'll see it has the the application info that we have that we output put our name we'll get some let's get something that's not a number and then we'll start guessing numbers and correct you say yes you want to play again correct if you say no enter it's going to exit all right so we've created a Windows console application hopefully you guys enjoyed this and hopefully it wasn't too confusing I know it was a little more difficult than doing like your standard hello world but I wanted to create something that was actually you know a little bit cool so that's it guys I do plan on having more c-sharp videos and more higher-level language videos in the future so hopefully you like this kind of thing and that's it thanks for watching you
Info
Channel: Traversy Media
Views: 889,830
Rating: undefined out of 5
Keywords: c#, c sharp, .net, c# .net, c# tutorial, c# app, windows app, ide, visual studio, visual studio 2017, learn c#, c# programming
Id: GcFJjpMFJvI
Channel Id: undefined
Length: 55min 41sec (3341 seconds)
Published: Fri Jun 09 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.