10 Time-Saving Tips for Visual Studio 2019 (as of 16.10.3)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
there are new time-saving shortcuts being added to visual studio with each update in this video i want to highlight 10 of those shortcuts that are in visual studio 2019 as of version 16.10.3 now some of these have been added very recently while others probably slipped in in a couple earlier versions but they go under the radar so often i want to make sure we point these out and highlight when you can really save some time with these new features now if this is your first video of mine my name is tim corians my goal to make learning c-sharp easier one of the ways i do that is by showing you how to use the free tools you have available to you in a better way in a more efficient way if that's something that interests you definitely subscribe to this channel also check out iamtimcory.com where i have a number of start to finish full courses on learning c-sharp on your own okay now this video will have source code so i will include a link in the description to get that source code if you would like but really this is all about those time saving shortcuts so the source code isn't as important as the actual shortcuts themselves all right let's look at visual studio and we're going to start with a new project so let's do a new uh console application just to get something going so we'll say newconsoleapplication.net core we'll call this shortcut demo and we'll say shortcut demo app now we're actually going to add another project type to this in a little bit but for now this gets us started so the first one i want to show off once this gets started is let's say we're going to create a new class now i'll do it right in the same file we could create a separate file but i want to show you create in the same file so the first shortcut that's been added recently is if i say public class let's call this demo class now normally i would put curly braces and then hit enter and make sure those clear braces on the right lines and so on but there's now a thing called the smart line break which if you hit shift enter it puts those curly braces in and it puts your cursor in the right spot so i didn't type a curly brace i just hit hit shift enter this works with methods as well so public void test method i hit shift enter and it puts the curly braces in if i say if 1 is greater than 0 and i put shift enter it puts the curly braces in anywhere you're going to get curly braces if you hit shift enter at the end of the line it's going to add those curly braces for you and move your cursor into the right spot this is really helpful it's also one of those things that you might miss if you didn't explicitly notice it in one of the updates now this is a bonus one but i created this class in the same file as the program class we don't want to keep it there so if you hit ctrl dot while highlighting demo you can bring up your quick actions refactoring menu and one of those things is that we can first extract an interface or a base class which is kind of cool but we can move this to demo.cs click that and it will move that class out create its own file and put our class in that file so that's the first tip or shortcut that has been added to visual studio that's pretty recently it's in the 16.10 range somewhere so the next one we're going to have to turn on a setting so we go to tools options and we're going to go go down here to text editor c sharp and then inside here we want to look for advanced inside there we're going to say add missing using directives uh in on paste so this this right here add missing using directors on paste so let's check that box and hit ok now let's just say in my console application i create a list of a string called names equals new as a string right and i go oh wait i don't have the using for that we do our we select the um the red squiggly we hit ctrl dot and it says hey you want to add the using system.collections.generic well yes we do cool but if i were to copy this and then go over to demo.cs and come down here into my test method notice there is no using statements at the top of this line one is the namespace but if i hit paste right here notice it added using system.collections.generic but i pasted in something that it knew what the usings were so when you're copying and pasting code from one spot to another it goes hey wait a minute i know which using statements you need let me go ahead and take care of those for you and it puts them in automatically a little bit of magic there to make your life a little easier now if it doesn't know what using statement to add of course that would be a problem but if it knows the using statement it's pretty intelligent about which using statements you're going to want it's going to add up for you automatically so that's number two is that adding missing using directives when you paste code now the next one is another setting we have to change so tool options and in this same text editor c-sharp advanced we're going to scroll down and look for the show inheritance margin okay that's i believe down the very bottom yes it is all right now this is marked as experimental but we want to show inheritance margin now i'm going to show what this does this is a it's a pretty cool feature so let's create some inheritance here i've got his demo class let's create a new class we're going to call it um better demo and we'll make it public and we're going to inherit from demo okay so inherit some demo and we don't have to put a method here that's not the point now let's create another class and we're going to say different demo and make it public as well and we're going to say inherit from demo as well okay so now because we have added that that check box all right let's save all those let me add that check box let's zoom in on the left margin and you'll notice this little thing right here and what this has is a little blue box with arrows up and down in it and it has an up white arrow okay that's saying that this has a parent so if we were to zoom out and we were click on this it says demo let me see what what what does that mean demo well you click on that it's going to take you right to the class that inherited from this is the parent class for our different demo now you'll notice over here that demo has that same icon only the white arrow is pointing down if you can see that and if we click on that we actually have two different items and it's saying here's the two classes that inherit from demo better demo and different demo which one you want to go to let's go to a better demo click on it and it takes you right to that class so a little feature but at the same time it's showing you the inheritance structure now let's use the same margin as our break points so if we were to have a break point in here like so it's the same margin as the break points which you may say wait a minute then i can't put a break point at that spot you're correct but you couldn't anyways because this is on the class definition itself it's not on anything actionable so we wouldn't put a breakpoint there anyway therefore they can reuse the same gutter and not take up more space in our ide so that's our third new shortcut and that is it that we can see the inheritance margin and take us to the different spots where we have that inheritance relationship yes we can do this with the f12 and shift f1 12 and control f12 but this allows it to very quickly navigate around and the part that i like the best is the the parent view because you can see all the places this is inherited i like that a lot okay so that's number three for this next one we need to add some references we don't need now we're not talking about the using references up here which these are just saying hey we're going to refer to something we already have installed these are just a shortcuts remember we're talking the actual references so in dependencies let's just say i add oh let's add a nuget package a few new packages ones we don't need let's add json newtonsoft.json we're going to say ok to that and let's add another one let's add automapper okay now i'm not going to use these i'm just adding them to my project now this is going to create a bit of a bloat on my application it's going to add dlls that i don't actually need so if i right click on my project i now have an option called remove unused references if i click that it's going to say hey you know what you have two projects or two packages automapper and newtonsoft that you are not using do you want me to remove those this is really helpful in projects that have grown over time what happens is they grow and they change and they morph but you're never really sure are we using that somewhere are we still using that and we went through the upgrade from.net framework.net core and maybe maybe we don't need those things anymore but we're not sure we don't want to touch it because if we touch it it might break so this way it's going to scan your code see what's not being used and it's going to say hey you know what we can remove those things for you do you want to do that in this case well sure i do we could say keep if you wanted to now notice this says packages it's also going to look at other types of references as well including class library references or references to dlls and other ways we're referencing um code that we're bringing into our application so we hit apply this cannot be undone if you want to do it yes we do now if we go back to our manage nuget packages we look at the installed packages there are none they're gone okay so that is my fourth really big helpful shortcut and that is to remove the unused references number five really takes its own video or it's gonna need its own video to explain fully i'm gonna do that in the near future but i'm gonna talk briefly about editor config editor config is a way to enforce a style guide and some other rules around your specific project or solution and it's one it's a thing you can pass around put in source control and say for this solution and all the projects underneath these are the things i want to have applied i want this i want this type of spacing i want this type of um error handling i want this type of um you know these things are warnings these things are errors the problem was that up until now editor config was a blank json file where you could add in the rules that you knew about and that you wanted applied but that's not a great that's not great for self-discovery that's not great for understanding what you could do it's great for once you know what to do you can easily do it the problem is it's a newer technology and we're not necessarily sure how to use it so it gets under utilized well now i can right click and i can do that at the project level or i can say add edit or config or i can go at the solution level and say add editor config let's add the solution level notices solution items and now we have this nice user interface that shows us exactly what the options are by default and allows us to change them so let's look at a few of these so for example use tabs or not using tabs using spaces now there's a little bit confusion about tabs versus spaces i use my tab key it's called a smart tab and what happens is it puts the amount of spaces in that i call one tab by default that is four spaces let's just say that i want to change that tab size to two and the indentation size to two as well tab versus indentation it's saying hey you want one tab which i do i want one tab per indentation not have to hit twice so we're gonna say the tab size is two indentation sizes two as well that means two spaces normally it's four so if you go back here to let's go to demo notice that um this public class demo right here right let's count the number of spaces one two three four there's four spaces right there now if i were to hit tab notice that it's actually spaces one two three four and the other way as well one two three four okay so that's what my tabs have been doing by default with the edit config i can say make that two instead and it's gonna apply for this project the cool thing is it will override your visual studio settings for your defaults which means that you can say for your team hey we're going to do these settings but that doesn't mean you have to all go in and change your visual studio settings you just need to have us edit config file in your project it also means you can work on different teams that have different standards and this will apply the correct standard for the correct team without you having to think about it there's a lot of options in here notice we can scroll down and read through all the different options for example insert space after comma yes i love that definitely do that but then this is just the formatting tab notice is also code style tab where you can get into a lot more code style for example do you want using statement simple using statements yes or no a simple using statement um is the one where it's newer style where you say using and there's a semicolon at the end it does not create the curly braces so that you can have less indentations and notice prefer simple using statements yes make that a suggestion so that when you have a using statement that has curly braces it's gonna say hey do you wanna make that a simple using statement or i can say you know what no make that an error and then it's going to stop the application from compiling until you fix that which that's a little extreme okay but that's a possibility so a suggestion is going to be here in our error list these are suggestions notice the five messages okay now i have messages in here right now ide zero zero four zero accessibility modify required um that's saying that i don't have the um the pub the uh yeah public or private on class program okay so these are things we're already used to using but now we can add our own in here and say these are the things i want to require make sure you do an is null check make sure that you have or prefer an object initializer there's a lot of stuff in here we're not going to go into all of it in this video it's way too deep for that but note that it's all here and it now has a gui we can actually click on these things read them over see they say if we have to we can look up what an expression body for constructor means and then go yeah i want that or no i never want that and these are defaults right now we can make any changes we want there's also analyzers this is where notice all of our id codes and there's a lot of them you can make changes to how your application deals with all these different id codes so do not declare event fields as virtual that's a suggestion right now you can say no you know what that should be a warning all right do not expose generic lists that's a warning right now we can say no let's disable that and you can make these tweaks and changes as fits your environment but the nice thing is they're all listed here you can go make your own changes you don't have to go in the edit or config and do it manually in json now i have applied a little bit of a change just these tab sizes now notice that didn't do anything here but if i go better demo and i were to say hit enter notice it actually hit enter and put on the same line and i'm like well that's not right because it should be it's tabbed in right well actually it's on the same line because that's the indent level for the new spacing but this isn't right because i now have four spaces here but it's saying that's actually two tabs not one because the fact that we're using the new system and i could manually fix this or i could even go and go to uh i believe it's under advanced format document like that and that fixes the document now it's it's fixed right and now the the cursor is in the right spot and if i were to type you know public void um testing shift enter like that it would work properly and put the right spacing in for now my two spaces instead of four per tap but i don't want to do that manually all the time even go down and find that that shortcut well we've got a better solution this is the next one of my um hints this has been around for a while but something that people often miss because it's so tiny and so hidden so that's right down here in the bottom right there there's a little broom with a drop down i want you to click the drop down and you have run code cleanup profile one profile two or configure code cleanup now i'm on a demo.cs file which has not been formatted if i just run code cleanup profile number one done it has taken care of all that formatting anything cool okay you just ran that shortcut different way to run the shortcut no it's not let's go look at the configure code cleanup and look at profile number one format document yes that was the the um the tweak we ran with the edit advanced and the rest that the um menu shortcut but then we also have remove unnecessary using statements we also have sort usings we also apply file header preferences okay which we don't think we have any file header preferences but that didn't really solve any problems for us we didn't have any actual using statements but let's go over here to different demo and click the little run code cleanup profile one check that out using statements are gone it has cleaned up our code as well we've done some really cool stuff four steps in one but if we look at the configure code cleanup look at the available fixers there's a list of fixers in this box sort imports remove unnecessary imports make private field read only when possible so a lot of remove unused variables there's a lot of stuff in here i wouldn't add this one by the way but a lot of stuff in here you could do and just you know select the ones that you think that are valuable and say yep put that in the included fixers and now it's in profile number one we hit okay and now whenever possible it's gonna make any fields private read-only if that makes sense so with your editor config adding those features using code cleanup will help ensure some of those changes that you have made may have made to things like spacing get applied right away and quickly but also other things as well now i did mention that i wouldn't remove unused variables that's one of the options that's in here well the reason why i would not do this one is because the fact that your unused variables tend to indicate that you were starting to do something and then you lost track or got off track and forgot to finish it if you just remove those you lose the lose the indicator that maybe you've missed something okay so i wouldn't do that one yes it would be kind of good for your application and not really because the compiler is going to do it for you but what it's going to do is it's going to hide the fact that you create a variable but never used it for some reason which means you changed your mind or you forgot to finish that part of the code so i wouldn't do that one because code cleanup really cool stuff right there all right so that's number six number seven let's create some properties in here okay so i am going to create let's um let's create a property prop tab tab string first name prop string last name okay so i have two properties now what if i want to bring these values in in a constructor so constructor you pass in a first name and a last name and i put those into the properties as initial values well i could come down here and say ctor for constructor then i could say string first name i'll just do one and i can say first name equals first name right that's the kind of the default pattern you would do but that's a lot of retyping the same things and getting the right order and it's just a pain so let's not do that instead let's select our property say ctrl dot and say generate constructor different demo string and now we have a constructor public different demo which is a the constructor and it says string first name notice the capitalization and it says this property equals this value cool now last name control dot and say let's add a parameter to different demo string or an optional parameter and now let's show you what the differences are let's add an actual parameter and there we go so now we have our constructor that takes in a first name and last name and assigns it to our properties pretty cool stuff right now let's do the same thing over again let's um actually let's um let's move this down if you don't know how to do that by the way if you select code like this let's say i move this above first and last name hold my alt key after it's been selected hold my alt key push the up arrow key and move it where i think it's going to fit best fit that right there we're good to go okay so you can move it around wherever you want so that is the uh one way to go about it you create your properties and then say hey i want to create a constructor that brings in this value either as the actual value or as an optional parameter um depending what you want that's one way of going about it but let's go to better demo and in here let's create a property actually let's not create a property let's create constructor let's create constructor ctor we're going to say string first whoops first name string last name ctrl dot and we're going to say let's create and assign remaining as properties click it and now we have two properties first name and last name so let's create our properties based upon the values you passed in notice it only creates the gets for these not the sets because it is setting a value in the constructor therefore we don't have to have a set if we don't want to so it's it's saying you know what by default make it as locked down as possible and you can unlock it and yes i want to unlock it so that i'd put set here for these two okay but now i have created these properties based upon the constructor parameters instead of creating the constructor parameters based upon the properties either way works also note that it has named things properly meaning i use camelcase for my parameters that's the the standard in the industry and then it says okay if we're gonna create properties we're into the same name but make it pascal case which is the first letter of every word is capitalized instead of having the first full word lowercase so it handles that conversion back and forth so that's the shortcut number seven which is adding properties to constructors and adding or creating properties based upon your constructor kind of back and forth both ways now let's keep going with this theme and look at number eight which is we kind of want to test these values right maybe this is coming from what a user creates or something like that we want to make sure that we take in only valid data we don't want to have let's say a null for our first name or something similar but how would we create that well what we do is we do a check so we'd say you know if we type the whole thing out but you're probably catching on by now we don't have to do that because we can right click on our first name go to quick actions refactoring or just ctrl dot when selecting it and say add string is null or white space check or is null or empty or a null check let's do and we can add null checks for all parameters let's click that one and it creates two different null checks is nuller empty for first name is null or empty for last name now what you do with this depends on how your application designed but typically you throw an exception and so microsoft says hey you know what we'll throw an exception you can change if you want to but we're going to try and make it as good as possible so argument exception which is a the right exception to throw and we're going to use string interpolation and pass in the name of the parameter so first name that way if we change first name is going to change it here so and puts it in single quotes cannot be null or empty and it's going to pass in the the correct name of there so we're going to have the right information and including this exception has a thing that's going to look at which per which argument was the problem and that is going to be where it pulls this first name in again so that's why it pulls it in twice once for the message and then once the exception knowing which argument was the problem so really cool stuff it does it automatically for us yes you can modify it yes you can change the logic but with one quick action refactoring i was able to do null checks on both of my parameters in just one step so that is number eight which is the test your parameters by just do our quick actions and refactoring now number nine let's say that this first name which is an auto property has to get in a set let's say i want to convert that over to being a full property well ctrl dot if you haven't follow along here yet ctrl dot for your quick actions refactory and in here we have a the idea of convert to a full property click that and it doesn't seem to do a whole lot it just says hey get an arrow with an underscore first name and then set first name equals value the arrow and it does create force at the very top private string underscore first name which follows our naming convention and that's our private backing field for our new full property you may say yeah but i want to do some work in this setter and now i've got to put the curly braces in all the rest just clicks the set control dot and say use block body for accessors there you go so now it keeps on the same line because they were on the same line before just hit enter now you've got your block by to make modifications for your set your get is still the one liner because you don't need to have any more than that if you don't want to now if you want something better ctrl dot use block body there you go now you can make your modifications to whatever you need to in your getter now let's say you're you're in here and go wait no that was the wrong one i didn't want to do that or who created this full property with the private backing field and all the rest i don't need that well click click on first name control dot use auto property instead boop back to where it was and notice that private backing field is gone so you can go back and forth between full properties and auto properties without having to rewrite your code so that is number nine in our quick shortcuts okay now number 10 i need to show you a new project type now this works on any razer pages file but we're going to add a blazer project this is the most useful in blazer so let's add a new project and we're gonna add a blazer server app is fine we're gonna call this um blazerdemo.net5 no authentication yes https noaa docker let's just create it uh by the way there's a whole lot more going on dockers new stuff and really cool stuff around docker and docker compose and being able to work with the containers and images and all the rest a whole lot easier in the newer versions of visual studio i'm not going to touch on that today but i just want to point that out okay so i have already added this or changed my visual studio for this but you will have to change it it does require a restart of visual studio which is why i already did it you go to tools options go over to environment look for preview features and in here this option right here to enable experimental razer editor okay there's a new razer editor they've been working on i had it enabled for a while i disabled it because it had some quirks it was um very experimental and you may still find a few quirks i haven't found the same quarks i was having so they have fixed those this is much more mature than it was but note though the keyword is still experimental does not mean you can't use it in production you can absolutely use it in production but if you have weird issues with razer come in here uncheck this box restart visual studio see if that clears up the issues okay just know that's the case but it's not going to change any code in a negative way it's not going to impact things that way it's just that they're still working on how to set this razor editor up for all the things they wanted to do okay so that's why it says experimental but it's not going to hurt you to check the box it just may mean you have to uncheck it again at some point which i did before but now i've rechecked it and have been using it okay so this does require a restart of visual studio which i did all right now if we go over to let's choose um let's go over to the index page and in blazer we have some pages created for us we have the error page the fetch data page the index page and the counter page right well with this new system we have a few things that are really cool first of all if i start typing let's say edit form but i'm not sure what exactly i want to do or what the options are intellisense is showing up but down here let's see if we can zoom in down here this little icon right here it's got the at symbol um it says only show tag helpers so some of these things are just html stuff like diff right but tag helpers are 4c sharp stuff so if i were to bring that back up ctrl j by the way i bring it back up i can click on that little at symbol and it filters it down to just the things that are c-sharp like that i might be interested in for example input checkbox input date input file these are the things that you might have had to kind of memorize or know were there now you can look through intellisense and find them notice survey prompt that's actually a custom one to this application but it's found in the list but then even further if i hit edit form i hit space it's now going to again click that little button the tag helper it's going to say hey you want probably a model and then there's some events here for on invalid submit on submit and on valid submit and so you can know what the options are for any item you're adding using that filter to show you only the things that are c sharp s or c-sharp related in your list so that's one really cool thing but then this is i think the number one improvement that razer the razer engine needed okay now just a quick note razer pages and razer engine are two separate things the razer pages uses the razer engine but blazer pages uses the razer engine as well that's why it's confusing yes i get it razer engine is all about mixing the c-sharp with the html that's what its job is okay so the razer engine had this flaw in that it didn't operate like the rest of our c-sharp code for instance if i'm in better demo and i go click on demo and i hit f12 i go a demo because f12 does a lookup for that item well now if i'm in the razer engine i click on survey prompt and go where is that hit f12 it takes me right there and notice it even highlights it over here on the right that's because i have visual studio set up to say open the current document in the solution explorer uh list so it takes me right to where survey prompt has been declared so i know right where to go you guys hit f12 and it takes me right where i need to be and this works no matter where you are so if i'm in main layout and i go oh where is that nav menu f12 there's a nav menu and it pulls it right up so this allows you to navigate through your blazer project especially uh with relative ease on the razer page side not just the c sharp side this is a huge performance boost for those of us who are building web applications using the razer syntax now these improvements are still ongoing that's why i said experimental but it will do things like add missing using statements to your blazer project pages and other things like that that you may have had to do manually in the past so really cool stuff here i think that was well worth the the effort they put into it they've definitely made a huge improvement in how we work with web projects so that's number 10 which is the razer auto complete the f12 you know finding the objects that alone was worth it but then also the basically bringing the intellisense and telecode and all the rest to our our c-sharp in the html the razer syntax side of things okay so that's number 10. we've got all 10 now there's a lot more i have other videos and other tips and tricks with visual studio i try not to step on those toes i try to cover new things so definitely go check those out those still apply there's still some really cool stuff in there that we didn't cover in this um video for example the pace json has classes that's some really cool stuff there and so on those are covering my other videos on visual studio tips and tricks are there others that come out recently that that you thought i may have missed or that we should add to a future list i'm going to do this again because i keep adding more and more stuff here um so let me know down in the comments let me know if if i missed something or if there's another one that that you really like that um that you use all the time okay thanks for watching and as always i am tim cory [Music] [Applause] [Music] you
Info
Channel: IAmTimCorey
Views: 75,632
Rating: 4.9504132 out of 5
Keywords: .net, c#, tim corey, iamtimcorey, i am tim corey, i am tim corey c#, i am tim corey blazor, visual studio 2019, visual studio 2019 tutorial, top 10, visual studio tips and tricks, visual studio productivity
Id: b4T63cdGs0U
Channel Id: undefined
Length: 40min 55sec (2455 seconds)
Published: Mon Jul 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.