C# and Visual Studio Code Beginners Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone I'm Gavin Lon yesterday I decided to create a kind of low Fidelity video to demonstrate getting started using visual studio code and creating and debugging c-sharp applications using visual studio code so I decided to just jump on the mic hit the record button and very spontaneously create this video this is not the way I usually create my videos I usually spend a lot more time on production quality so this is very much an experiment for me the advantage of this low Fidelity type of video is that I'm able to present you with informative content without the time overhead required for more High Fidelity video production so the advantage of the style of video creation is I can get educational content out to you a lot faster so I'll be able to provide you with a lot more content on this channel please let me know in the comments section what you think of this more spontaneous Lo-Fi style of video creation and please don't worry if you like the more high quality production videos I will still of course frequently create these types of videos on this channel to support the Channel please hit the like button and please consider subscribing and please ring the bell so that you'll be notified of future content please feel free to share this video with anyone you feel May benefit from its content if you're feeling generous and you'd like to buy me a coffee you can do this at my buy me a coffee webpage at this URL I love reading your comments so please feel free to engage with me in the comments section right let's get into this video right so the first thing we're going to do is install Visual Studio code we can just simply do a search for visual Studio code okay right and we've got download here we can just click on downloads option here we've got all the options here you can install it on a Mac this is the great thing about Visual Studio code it's cross-platform so you could install it on your Mac or whatever version of Linux and of course you can install it for Windows so we can go to the windows version there thanks for downloading vs code for Windows and it's downloading it's going to put it into my downloads folder great off it goes so then we can just go to the downloads folder and we can click on the executable to install vs code on the appropriate platform and you just follow the instructions on the wizard see I've got create a desktop icon I find that can be obviously quite convenient for when you want to launch Visual Studio code anyway I've actually got Visual Studio code installed the latest version of Visual Studio code installed so I'm going to abandon this process but please continue until you've got the latest version of Visual Studio code installed on your local machine great so then of course we can just launch Visual Studio code by double clicking it on our desktops we're going to create a project from scratch so I'm going to go to my C drive and just create a root folder for where we're going to where we're going to store our various projects so I'm going to call this development since I've already got it so I'm going to call this um I'm just going to call this source right and then what we can do is open this directory directly within vs code so open folder there we go Source our new folder that we just created in the C drive we go select folder for c-sharp applications we want to ensure that we have the c-sharp dev kit extension installed for vs code so to do that we just simply click on the extensions option here and then we can do a search for C sharp and we've got C sharp dev kit right here and you can see I've got the uninstall option available to me here because obviously I've got it installed but if you if you see install there means you don't have it installed so you press the button and it automatically installs the c-sharp dev kit extension for you I would say it's basically essential to install this extension if you want to develop c-sharp applications using vs code so make sure the c-sharp dev kit extension is installed okay excellent so let's go back to our Explorer window here in vs code let's launch the terminal window so we can do that by going to view hit terminal like that you can see that so the source folder is now our current directory so I'm going to show you how easy it is to create a console application so in order to do this we're going to use the dot net command but we first need to make sure that we have the.net SDK installed in order to leverage this dot net command so you can go to um Google put in a search for dot net SDK right download.net SDK for visual studio so all we do is we click this to install it we just click this button it downloads the install executable okay it's taking a bit of time but we'll get there to access the executable you'll see in a bit we just simply click on the folder icon go to the folder and you can see the the exe that we've got to double click on to run the install process so then we just simply double click on it and we click the install and you go through the instructions and it'll install the latest stable release of the.net SDK so I'm not going to go through with this I've actually got I believe currently what I've got installed is net 8 preview 6 of the.net SDK so this will be fine for the the demonstration conducted in this video so you can verify that you've got the.net SDK installed by running within your terminal window and vs code go.net dash dash version and press enter and of course I put ver V very on so that's not going to work so let's go dot net dash dash version and there we go we've got preview 6 of.net 8 installed excellent now what we can do is create a project using a.net command using the.net SDK so the command we're going to use we're going to use the console project template to create our project so we're going to develop a console application in this video just a very basic console application to give you an idea of how you create a c-sharp application and vs code using the c-sharp dev kit extension so here's the command for creating a new project within the source folder so we're going to create dot we so let's type dot net new console which is our Pro the project template we want to leverage so it's net new console Dash o essentially Dash o is going to create the necessary extra folders where we want our project files to reside so we go Dash o and then we can say C sharp project so it's going to create a C sharp projects directory within the source folder and then we want another folder the employee app folder like that and then simply press enter it's created that successfully for us so we can actually within our Explorer window we can see the various files that it's created which includes a CS project file which denotes our project our c-sharp project our c-sharp console application project and then we've got the program.cs file which contains the actual code so we could actually run that now by typing.net run and it's going to Output nothing because we are not in the correct directory that's why it's going to Output nothing so if we go CD and we go C sharp project so we have to be in the same directory here before we run the dot net run command we have to be in the same directory as where the Cs project file for our relevant project resides so let's navigate to that by going CD c sharp Project M employee app press the enter key and now we're in the correct current directory and we can go dot net run and hello world should be outputted to us come on there you go so hello world is outputted to us so we are on the right track and if we wanted to alter our code very simple we go hello C sharp for example here and then we can save our project by going file save like that and then we go done run here and whoops donate run donut run clever of me but okay so we're gonna go dot net run not donut run and we've got hello C sharp and that's what we want so hello C sharp is uh Prince to the screen so everything is in place now but now I mean you could also for example create a more sophisticated type of project so let's say you wanted to create a Blazer server project it's very simple so let's actually navigate backwards here oops and clear the screen so we're in the source directory here and let's run the command for creating a Blazer server project we're not going to develop a Blazer server project I just want to show you how you would create a Project based on the Blazer server project template using the.net SDK in your terminal window within Visual Studio code so it's very simple it's very similar to what we've already done with the console project so we go dot net we go new we go Blazer server like this Dash o and then the directory where we want our project to reside so we're going to create a Blazer server app project directory that's where our project files will be generated so let's hit the enter key and see what happens okay now if we go here c-sharp projects we can see we've got two projects we can see it right within the Explorer window of vs code and we've got the employee app project which is based on the console project template and then we've got the Blazer server project which is of course based on the Blazer server project template we've got all the relevant files here so this is the program file which is mostly configuration sort of code you know use static files use routing use Blazer hub for setting up the Blazer server application um so to make things simple to get into the sport we can actually run this because it creates a lot of default code for us so we can actually run this Blazer server application which has obviously minimal functionality but it will be enough to show us how to set up a Blazer server project and then run the run the project so we can simply go to the go directly to the folder where our Blazer server project is installed so if we go to so if we go to Source here c-sharp projects we go Blazer server and we directly open that folder and then we launch the terminal window okay we can now actually just run this project so I've got net run see what it does here great so that looks good so now it's listening at this location HTTP colon slash localhost Port 5271 so if we just copy that into our favorite browser we can now see our Blazer server app with this is just default functionality that's been generated for us but you can see it all in action there and that's that's it you basically created a Blazer server application and you can just create your code create your application from there brilliant so you can see you're pretty much unlimited you can do almost the same things you do in Visual Studio you can do now and vs code um so you can create a multitude of different types of applications it's really straightforward using the.net SDK and vs code let's go back we're going to develop a basic console project application let's go directly to the console project app all we do is we open the folder let's go to the app here we go select folder open the program.cs file and we can tap out our code awesome so all we're going to do is create a simple application that accepts two inputs from the user so it's going to be the first name and the last name of an employee and then it'll basically add it to a list which will be displayed to the user and the reason we're going to create this application is so that you can see some of the debug features in action okay excellent so let's get going with this so I'm going to first create a variable named exit it's a Boolean variable and I'm going to initialize it to false you'll see the meaning of this variable in a bit and let's create a string variable for the first name and we'll just initialize that to an empty string and then we want the last name and look at it it's predicted that for us that's the one of the benefits of having the c-sharp dev kit installed it's predicted what variable we want to add next so I'm going to just press the tab key right so let's continue now we want to create a generic list so this is going to store the collection of the employee data in a strongly typed list so let's create a list a generic list and we haven't yet got the employee class here so we can't strongly type it so let's actually create our class here quickly and it's very simple it's just a very simple class so we all to create our class all we do is we're a class whoops we don't want to do that class employee perfect there and then we open close brackets and we're just going to public right string first name it's predicted that we want to include our first name field perfect so we just press Tab and it adds that in for us and then we can just make a duplicate of that and call this last name like that and we've got our class so now we can create our list and we can strongly type it so we create a generic list type and we put employee within the angle brackets to strongly type our list Visual Studio code is outputting a prediction of the line of code that we want to create and it happens to be correct so let's just press the Tab Key and there it is that's really convenient we're going to include a do while loop that we want to execute infinitely while the exit variable equals to false so once the exit variable equals to true we want this do while loop to be exited okay we've got a lot of predictive stuff coming up there which is really cool so we want to clear the screen first then we want to write a prompt please so this is going to be outputted to the user please press the a key to add an employee comma or press any other key to exit the app location okay that's as simple as that and then we want to read whatever the user presses and we can do that with this line of code so we have console key key equals to read key dot key and then if the key is the a key we can write functionality to add the entered first name and last name of the relevant employee to the employees list so we want console key dot a like that so checking whether the user has pressed the a key and if the user has pressed the a key I'm going to put right line like that so it's just going to write a blank line to to the screen it's just really for formatting we can just do it like that there with an nothing passed in as an argument to the right line method like that and then we can write another prompt and this time we want the user to enter the relevant employees first name so we'll go here please enter the employees first name that colon there just for formatting and then we can read the user's input into memory with this line of code using the read line method so it's con Sol dot read line like this and you can see we're getting some squiggly lines there we're going to use the double question mark So if if the console.readline is null we're telling the compiler we're assigning an empty string to the first name variable like this and then we just want to prompt the user now for the last name value last name like this so we go and then we go VAR amp equals to we're going to create a new employee object and we can do this we go first name equals to first name and last name equals the last name so it's predicted that perfect for us that's exactly the line of code we want so we now have created an employee object based on the user's input of first name and last name which is exactly what we want right and then we just simply add it to the employees strongly typed list with this line of code brilliant okay so if key equals to A so if the users pressed a we want to add whatever the user inputs as the first name and last name to a list of employees else we want to set the exit variable to True which means it'll exit the while loop and the application is terminated so the application is finished the user has basically added all the list items and is happy with the display and wants to finish the application so presses any other key to terminate the application brilliant and that is pretty much the logic for our application I guess we can just test that now and see what happens so as we know now we first have to save the program.cs file which obviously contains our code um view the terminal window okay and we go dot net run and see what happens okay please press the a key to add an employee or press any other key to exit the application so let's press the a key brilliant so it's now prompting us for the employee's first name so I'm just going to put in my own name Gavin Lon and then if we press enter oops so we're currently not we're adding it to the list but we're not displaying it so we want to display the collection to the user but we're on the right track here so let's press Ctrl C to cancel that clear the screen okay and now let's create a method that outputs the list of employees to the console window so I'm going to create a method just that doesn't return any value called display employees that okay and then we're just going to do a four each Loop that Loops through the list of employees so we want to go employee right predictive code it's come up for us and it's absolutely dead on and that's what we want so this is really convenient for us and then we're just going to write it to the screen like this and we'll just use a right line so right line basically appends a new line to the end of whichever parameter whichever argument you're passing to the right line parameter here we're going to pass in an interpolated string so employee DOT first name and last name that code will output exactly what we want which is the first name of the employee followed by the last name of the employee Okay so right then we can just call that method up here and let's see what the result of that is okay so dot run a first name Kevin last name Lon here we go so we've got gavinlan printed so we've only got one list item one employee and our strongly typed employees list so it's outputting that employee which is absolutely correct the details of that employee Gavin long to the screen so let's press a again let's add a new employee so let's call this guy Bob Jones and there we go we have Gavin long and Bob Jones as employees added to our application added to our employees list let's press say again to add a new another one so let's go Sally Lane and Sally's details have now been added to the employees list and output to the screen so our little application is working pretty well let's exit the application by pressing any other key and there we go so we've got our application our console application is working pretty well that's great so and now what do we want to do I want to show you how we can actually replace the class the user defined type here with a record rather and we're going to do that for a particular reason and it's to do with comparing equality so basically we only want an item added to our employees list if they don't already exist in that list so if we did a check now to see if that employee exists in that list using the contains method this would not actually work because this type is a reference type we need a value type in order to do a direct comparison of values for this purpose so let's just show firstly how this won't work you using our as it is as our code is in its current state using a class but when we use the record type it will work because it now will behave like a value type rather than a reference type so firstly let's enter the incur well the incorrect code so before we add the employee we want to compare the object to all of the objects currently within the employees list and to do that we can use an if statement and the contains method like this so we go if not employees dot contains amp so if it doesn't already exist in the list then only add it this should work intuitively or everything looks good but it won't work because an object derived from a class is a reference type and when you do a comparison like this or an object it's not comparing the actual values of the first name and last name to these values in the objects that reside within the list and that's what we want so this this code here is not going to not going to work as expected so if we go dot net run we go a we go Gavin lawn it adds Gavin Lon to the list as expected but now what happens if we add Gavin long again we don't want this item to be added to the list because Gavin long already exists in the employees list so it is adding it which means it's not working so how do we get around it in C sharp 9 the record type was introduced which means that convenience has been provided through this new record type so to create a record type it's actually a lot simpler than to create a class the code is a lot more concise you would perhaps want to use the record type if your user-defined type was only ever going to contain data and no behavioral functionality so to create an employee record type you can use this code which as you can see is very simple and that's it that's our record and now so it's complaining there because the casing is different so we can just we could just do this to fix this just go first name and last name it actually synthesizes these properties for us behind the scenes we need to call this a little bit differently so it's literally like instantiating an employee object and passing the relevant values to the Constructor rather than assigning the values to their properties so to do this we simply alter the code appropriately like this and now let's see if let's see how our code behaves now if it's doing what we want it to do so let's first clear the screen dot net run okay press the a key because we want to add a new record Gavin Lon Okay so we've got Gavin lot it's the code is giving us is the output that we would expect so let's see if we can add Gavin on again so we don't want gavinlan added to the list because it already resides within the list oh and it has added it and the reason is I didn't save my code so that's why that's not working I forgot to save my code so always remember once you've made a modification to your code to save it okay hopefully that is the reason why I'm gonna look silly but okay let's anyway let's cancel out of that let's clear the screen and let's go Dot net run a Gavin hold on okay let's add another one and it doesn't add at this time so our code is working because now our employee type is behaving like a value type and not a reference type so it compares the actual values of first name and last name to the relevant values of the objects that already reside within the employees list so that is working perfectly won't allow us to add gavinlan again to the list but we can of course add Bob Jones there he is and we can of course add Jenny Lane can we add Jenny Lane again let's see if we can add Jenny Lane again Jenny Lane we can't so our code's working as expected the next thing I want to look at is how we can actually debug our code okay so let's cancel out of that there clear the screen and let's see if we can debug our code right so how do we debug our code well it's actually very simple because we have the c-sharp dev kit extension installed we can actually just press F5 and it'll run our code for us and to test that let's put a break point around this Mark here no let's put it there display employees just to see that it breaks that our code is debugging so if you look here we can go to this option run and debug option here so we can see the locals watch and stack Trace Windows appear in front of us and we can you we can leverage these windows to assess the state of our application at a given point within the running of our application so let's see this in action let's press the F5 button here to run our code okay so it's coming up with the various options here let's select C sharp within our Command palette okay brilliant and it's going to run our code for us now and will it hit our breakpoint okay so it's got a problem here because when we're in debug mode it's using a different window to the actual terminal window that we wanted to use so we can actually get around this by changing a set setting but you can see that it has gone into debug mode you can see the locals window the watch window and the call stack window here so to get past this bug let's first stop our code from running by clicking that button there go back to the terminal window okay and then we can go to the settings here and we need it to change a particular setting so it's going to search for console like this this option is only used for the.net debug configuration type so we want to configure this differently so that our code will work so what we need to do isn't is set this option to integrate a terminal and then we can debug our console application it's got a problem with the reference to console there so that's it so now we now that we've changed that setting it should work as expected let's press F5 there we go let's set our breakpoint brilliant okay so now we can just debug it as you know as you would with say visual studio so to step over the code we could press for example F10 and it steps over the code we've stepped over so we can step through our code as normal now we're in that mode and we can use these we can leverage these windows to assess the state of certain variables we can assess the state of our application by assessing what's displayed to us in these windows like the locals window and the watch window and we can set up various things within say for example the watch window so for example in the watch window here we can assess the state of the exit variable by adding this expression so we go exit equals to true and see what that does let's put another break point here let's go Gavin one brilliant you can see here now within the watch window the current state at this line of execution the current state of the exit variable equal and true is false because the exit variable equals to false I mean you could just monitor the variable by putting in the variable name itself but I just want to show you how you can actually include expressions and we can also look at the other variable the state of the other variables too so right the state of the first name variable is currently Gavin so this is how you can use the watch window and you can see here we've got the state of the variables outputted to us in the locals window here but you can include actual Expressions here within the watch window for more sophisticated assessments of the state of your application at a particular point in the running of your code great and that's pretty much it that's how you are able to analyze your code as you're debugging and running through it using the locals window the watch window and the pull stack window so within the the entry point of the application we're on display employees within the actual employees method which is the item stacked on top of the main method the entry point you can actually navigate to where the code is executing within the display employees method here so we've got the stack displayed to us in this window here and we've got various status information about our variables so we can see various information about the state of our application at a given point at a given line about to be executed within our code so let's take out that break point and let's press F5 to continue executing our code go to terminal press a like this Gavin the loan and it outputs Gavin long but this should give you an idea of how you can actually debug your code a again Jenny Lane I hope you enjoyed this rather spontaneously created video on how to create and debug c-sharp applications using visual studio code I hope to see you soon thank you and take care [Music]
Info
Channel: Gavin Lon
Views: 1,954
Rating: undefined out of 5
Keywords:
Id: rab_1cFQUF4
Channel Id: undefined
Length: 38min 18sec (2298 seconds)
Published: Sun Sep 24 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.