Creating Code Snippets in C# - Customize Visual Studio for Efficiency

Поделиться
HTML-код
  • Опубликовано: 7 сен 2024

Комментарии • 155

  • @jimsmith717
    @jimsmith717 4 года назад +18

    Thanks, Tim!
    Another advantage of snippets is that they help me stay focused on what I want to do, rather than trying to remember the "how".

  • @RalfsBalodis
    @RalfsBalodis 4 года назад +27

    0:00 - Intro
    1:41 - Creating demo application
    3:00 - Code snippets: few built in snippets, ease of use
    6:00 - Creating a code snippet in VSCode
    14:18 - Import code snippet in Visual Studio
    17:31 - Creating a code snippet: quick recap
    22:40 - HTML snippet
    28:30 - Advanced snippets
    30:38 - Creating advanced snippet
    34:35 - Creating advanced snippet: Declarations - variables in the code snippet
    43:46 - Conclusions and concluding remarks

    • @IAmTimCorey
      @IAmTimCorey  4 года назад +4

      Thank you! I added to the video. If you folks appreciate Ralf doing this, please let him know!

  • @nynjalantz
    @nynjalantz 4 года назад +3

    I like to separate my classes into regions. It just makes it easy for me. So i made a snippet for that. The one im proud of is my method snippet. Just specify the return, name, and params and done!
    This inspired me to make a console app that copies my snippets into the VS folder for me because of how annoying the snippet manager can be.
    I got on the struggle bus about 3 months ago and figured it out, eventually. Im glad you made this. No more headaches. Thank you for basically teaching me everything I know. I cant be more thankful.

    • @IAmTimCorey
      @IAmTimCorey  4 года назад

      I'm glad I was able to be of help.

    • @wagnerthomas6780
      @wagnerthomas6780 2 года назад

      I did the same, but I left parameter part empty, the problem is I also tried to add an empty block part just to tab there "quicker", but it only switches between type,name,param

      param

      Parameters


      block

      Block Code
      Any idea?

    • @v0id_d3m0n
      @v0id_d3m0n Год назад

      @@wagnerthomas6780 i assume it doesn't work because u left it blank? Maybe try putting something like "myCode" as the default instead?

    • @wagnerthomas6780
      @wagnerthomas6780 Год назад

      @@v0id_d3m0n I tried that it didn't worked, besides I already got accustomed to this one so I'm just pressing enter after snipet and it's jumps in the code block section.

  • @ShawkyShamy
    @ShawkyShamy 2 года назад +1

    This video deserves receiving more likes !
    Thank you Tim for the effort

  • @JasonWynn
    @JasonWynn 4 года назад

    Hi Tim,
    I’ve been using code snippets since VS 2005 and in my opinion, they are the single greatest thing that Microsoft has added to Visual Studio. I’ve written code snippets to outline entire UI pages (search, view, add, edit, and audit pages), my code behind (yes, I still use WebForms) and business logic classes. Because these snippets are large, I found that putting a comment at the beginning of the snippet that includes all of the parameters contained within the snippet very useful. I include several TODO: comments and commented out examples to remind me of tasks I need to complete and the syntax I need to use when working with certain objects/methods. Since my snippets references other classes/methods outside of the snippet, establishing a consistent coding pattern and naming convention is critical as it allows me to make certain assumptions when creating my snippets.
    Snippets have greatly reduced my coding time and made my applications a lot more consistent than it would be if I had to create everything from scratch each time I added a class.
    Thank you for all of your videos. I have found them very helpful.

    • @IAmTimCorey
      @IAmTimCorey  4 года назад

      Awesome! Thanks for sharing.

  • @heshanlk17
    @heshanlk17 4 года назад +3

    Really appreciate your work Tim! One of the best (probably the best) C# tutorials available on the internet!

  • @jameslacey3674
    @jameslacey3674 4 года назад +1

    Great video. I can now layout regions in my classes, add methods, and specify fields and local variables by simply typing a couple of characters and tabbing twice. It really saves a lot of time. Thanks alot.

  • @mantis2k11
    @mantis2k11 4 года назад +2

    Another great in-depth tutorial! Wasn‘t even aware of using such a cool feature, thx!

  • @nrnoble
    @nrnoble 11 месяцев назад +1

    Thanks for the video. I worked at MS for 15 years, the state of this feature seems like it was never completed. The core part is there (ie, it works), but missing is the snippet editor. It is like a summer intern was working on it, but left before it was completed. As it stands now, even in VS 2022, every change has to be re-imported; can't go back and simply update the XML file. VS Code does a better job with snippets.

    • @IAmTimCorey
      @IAmTimCorey  11 месяцев назад

      That may be exactly what happened. The danger with big software projects is that items that aren't critical priorities tend to get ignored forever. In this case, 15 years of pain and frustration should elevate the issue over time but when you evaluate the "feature" that is needed in a vacuum, it is never that important compared to other priorities.

  • @aleksandarmatejic9272
    @aleksandarmatejic9272 2 года назад

    You are a legend! For every topic that i needed some instructions so far regarding C# you had a tutorial.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +1

      I’m glad my content has been helpful.

  • @DanaStephenson
    @DanaStephenson 4 года назад +1

    I was dumbfounded when you switched to VS Code -- why on earth!? But this turns out to be a great tip! A short explanation there would have been helpful, like "VS won't edit a .snippet and pretend it's just a .XML but VS Code will!" In a pinch, you can edit in VS by renaming .snippet to .XML and back, but that's so tedious. Thanks!

    • @IAmTimCorey
      @IAmTimCorey  4 года назад

      Yep, it is a nice text editor.

  • @richardmcnamee
    @richardmcnamee 4 месяца назад

    Hi Tim, that's a great video. I was searching for adding code snippets for adding Arrange, Act, Assert comments in a unit test which was always so repetitive.

    • @IAmTimCorey
      @IAmTimCorey  4 месяца назад

      I am glad it was helpful.

  • @garyfirzon
    @garyfirzon 4 года назад

    Created a custom snippet based on video and code samples. Thank you Tim!

  • @bohuang3122
    @bohuang3122 2 года назад

    It's a so wonderful course. I ensure I can speed my coding a lot. Thank Tim Corey for his elaborative course!

  • @alevsoft
    @alevsoft 4 года назад

    Ingenious. Just saved me 2 hours/day :)
    Made a snippet for xUnit [Fact] block. Jeez, things are going great now and way faster

  • @xhephergaming
    @xhephergaming 4 года назад +1

    This is great! Thanks for the video. I'll be using this, plus whatever I can learn about debugging to make my life easier.

  • @mrterrbl8184
    @mrterrbl8184 4 года назад

    Lol I was at 22:30 wondering to myself if I could use this for HTML and you were right there with me.

  • @peckdec
    @peckdec 3 года назад

    Thank you for the awesome video. In the end you talk about time saving, but I would like to add another (possibly even more important than time) benefit: it reduces decision fatigue. So in addition to saving time, it can add more productive coding time into your day because you have more mental energy left for the important stuff.

  • @DrWambua
    @DrWambua 4 года назад +1

    Man, you're just awesome. This is epic 🙌

  • @RawCoding
    @RawCoding 4 года назад +4

    a bit dragged out at the begining, but alot of awesome examples, thank you

  • @PaulFWatts
    @PaulFWatts 2 года назад

    Thanks Tim. Very well produced video, great content, and very helpful.

  • @nandkishorsonwale
    @nandkishorsonwale 3 года назад

    Great in-depth tutorial. It really save my time and defiantly increase productivity. Thank you so much sir.

  • @dalv2584
    @dalv2584 3 года назад

    Jesus christ can u just get to the point of the video
    Edit: the wait was more than worth it, quality content right there

    • @IAmTimCorey
      @IAmTimCorey  3 года назад +1

      Thanks for the feedback. I believe in giving context for what I am teaching and that takes time. My goal is not entertainment but true education. Please watch your language.

    • @dalv2584
      @dalv2584 3 года назад

      @@IAmTimCorey I noticed later in the vid how much effort and quality you put in the vid and forgot about the comment. I was quite in a bad mood when I left the comment, so I am rly sorry for my language.

  • @pkboy1112
    @pkboy1112 4 года назад +5

    This video helped me a lot.
    Thanks for the good work.
    I was wondering if I can pay the weekly challanges subscription with PayPal cause I couldn't do that. The only option was with credit card and I don't have one.
    Can I pay the subscription with PayPal?

    • @IAmTimCorey
      @IAmTimCorey  4 года назад +1

      Unfortunately, I don't control which payment options are available. I enable everything I can but it is up to the providers to support what I'm offering. I believe that my provider can't work with PayPal on subscription-based payments. Sorry about that.

  • @caseyspaulding
    @caseyspaulding Год назад

    This is a huge time saver. Thanks for sharing.

  • @patrickmagee774
    @patrickmagee774 2 года назад

    Fantastic, very helpful

  • @v0id_d3m0n
    @v0id_d3m0n Год назад

    Thank you, this was really helpful!!

  • @raj.mahato
    @raj.mahato 5 месяцев назад

    Thanks tim,
    Now how do i write code in same line?
    with this snippet it goes to next line pls help

  • @philippemiseur1415
    @philippemiseur1415 4 года назад +3

    Tim, check out Snippet Designer by Matthew Manela on github, might come in handy.
    I'm using Microsoft Visual Studio Community 2019 Preview Version 16.7.0 Preview 1.0
    and it's working fine (for what i need it for that is).

    • @IAmTimCorey
      @IAmTimCorey  4 года назад

      Thanks for the suggestion.

    • @ashtondev
      @ashtondev Год назад

      I can confirm it still works great in VS 2022 and I'm able to remake my vscode snippets in visual studio in seconds!
      ❤🥰

  • @beagley
    @beagley Год назад

    Anyone super struggling with this in VS 2022? All the steps so far (17 min in) are the same. Typing the shortcut shows the snippet in the list as expected, but double-tab does nothing (actually, it just double tabs!). Restarted VS, still no luck. Double clicking on the shortcut in the list also doesn't work. Right click Snippet menu is also not there.
    In Tools / Options / Text Editor / C# / IntelliSense I see some experimental changes happening, but haven't figured out the right combination. Anyone get this to work?

  • @eldonpreston8896
    @eldonpreston8896 4 года назад

    Awesome stuff! Thankyou, already speeding things up!

  • @stevewoods17
    @stevewoods17 Год назад

    incredible, thanks dude

  • @muddledlindy73
    @muddledlindy73 3 года назад

    Yes. So helpful, thank you. I now have snippets for Serilog Information, Warning and Error they even include the $"" for string interpolation. Such a time saver. Did you know you can have multiple snippets in a file? And, the snippet can also add a "using Serilog;" line for you as well.

  • @DJone4one
    @DJone4one 2 года назад

    cool. i have create with this demo... a using and namespace statement code snippet for the new VS 2022. Its good, because then i get the using directives too using System.......and more

  • @jonesaby4954
    @jonesaby4954 Год назад

    Your tips are amazing. I just have a query regarding snippets. You showed an example snippet with name date etc. Name/author: I can add my name. Is there a way where date : shows the current date?

  • @philgolden8619
    @philgolden8619 Год назад

    Strange thing... I removed the cw snippet and replaced it with one of my own so it inserts quotation marks inside then places the cursor inside the quotes. The shortcut will still use the MS snippet on cw
    Is there another place on disk that it is reading the old snippet from. I also did one for interpolation $$"{$end$}" and that works as expected every time however another one for Write() will place the cursor just before the dot after Console.

    • @IAmTimCorey
      @IAmTimCorey  Год назад

      I would recommend not trying to overwrite Microsoft's snippets. It might have been in the list next to the MS one or it might be flipping a coin as to which to access.

    • @philgolden8619
      @philgolden8619 Год назад

      @@IAmTimCorey I completely removed it to try and figure what is going on and it will still pop. It is not reading any cw snippet as I removed my own one too to see if it was conflicting. The snippet it is using now has Console.WriteLine in what I assume is the title tag.

  • @fiszle
    @fiszle 2 года назад

    You're a life saver Tim! Is there any way to expand snippets with the "Enter" key instead of double tapping "Tab". I've searched through every setting and couldn't find a way to change it. Coming from years of VS Code I'm used to autocompletions and snippets with the Enter key. I'm losing my mind trying to figure it out!

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      You might be able to change that in the key mappings, but I have never tried.

  • @andywalter7426
    @andywalter7426 4 года назад +1

    Do you think you can do a followup video on how to create snippets that is in use in blazor server side? Because I tried the html one for a blazor page (which is razor components) and did not show it. Blazor is probably being used more now than Razor Pages since you can do more with Blazor server side than with Razor Pages.

    • @IAmTimCorey
      @IAmTimCorey  4 года назад +2

      When I do my video on Blazor server-side, I'll try to remember to add that as part of the video.

  • @CiaranGallagher1
    @CiaranGallagher1 3 года назад

    Nice job

  • @mohammadsalehzarei7973
    @mohammadsalehzarei7973 4 года назад

    Great!
    Thanks, Tim!

  • @SvendKoustrup
    @SvendKoustrup 2 года назад

    I love snippets! Is it possible to create a snippet, that can take multiple carets or multiple selections of text (with Ctrl+Alt+Click/Selection) and either insert or surround said carets or text selections?

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      I don’t think so.

    • @SvendKoustrup
      @SvendKoustrup 2 года назад

      @@IAmTimCorey Bugger 🙂 I also love the multi caret features. All apps should have that.

    • @SvendKoustrup
      @SvendKoustrup 2 года назад

      Uh, one more annoying question: I often have to change a mthod from e.g. "public override string methodname" to "public override Task methodname" (but without making it async). I have a code snippet for this. But I also have to change all return statements to return Task.FromResult(previous_return_variable/value). Can code snippets "find" all return statements in the given method? Or should I instead look into how I'm creating custom code fixes?

  • @mankgwanyanetlaka5012
    @mankgwanyanetlaka5012 4 года назад

    Thank you so much Tim.

  • @fredrikstrid9922
    @fredrikstrid9922 4 года назад

    Thanks for a great video, did not know that the snippets was this powerful and configurable. When you create your own snippets and add them to the manager, is it just local or is it stored in your logged in account in VS?

    • @IAmTimCorey
      @IAmTimCorey  4 года назад +1

      It is just local to that machine.

  • @JohnNecirRebellion
    @JohnNecirRebellion 4 года назад

    @IAmTimCorey I used the "Import" tag you forgot to include, in my Newtonsoft Json decorated property snippet, so when it's generated it'll include a using statement if I forgot to.

    • @IAmTimCorey
      @IAmTimCorey  4 года назад

      I'm not sure where you think I forgot a using statement. My examples don't use Newtonsoft.json. My appsettings.json file doesn't need Newtonsoft, it just needs IConfiguration, which I assumed would be set up since we are using the _config variable that you can only get by creating it (and adding the using statement). Typically you do that in the constructor using dependency injection.

  • @AbubakrMahdiSan
    @AbubakrMahdiSan 4 года назад

    Thanks , very much Tim

  • @alekseev74
    @alekseev74 4 года назад

    Why text editor intellisense does not recognize shortcut i've set for my snippet? But i still can type shortcut without tip and snippet works properly by the way. VS2019.

    • @alekseev74
      @alekseev74 4 года назад

      It happens if you use Resharper Intellisense scheme because code snippet created outside Resharper area and cannot be recognized.

    • @IAmTimCorey
      @IAmTimCorey  4 года назад +1

      Interesting. There should be a way to resolve this but since I don't use Resharper, I don't know how to do that.

  • @landonconway79
    @landonconway79 Год назад

    Hey Corey, I tried to import my snippets countless times, even making the exact snippet youve made. I cannot import them because VS says they are "invalid".

    • @IAmTimCorey
      @IAmTimCorey  Год назад

      I'm not sure what the issue is here. Sorry.

    • @landonconway79
      @landonconway79 Год назад

      @@IAmTimCorey Hey Corey. Hats off to you for even responding. I made a typical dumb programmer mistake and spend about an hour trying to figure why it was not working only to find out that the syntax of my xml was incorrect. Quite an under accomplishment if you aksed me. I probably would not make it as a professional programmer. Lol.

  • @mrslake7096
    @mrslake7096 2 года назад

    life saver

  • @opaulodiogo1
    @opaulodiogo1 3 года назад

    Very good! Thank you!

  • @scotttct
    @scotttct 4 года назад

    great video, very helpful l Thanks!

  • @rahulpardeshi1073
    @rahulpardeshi1073 3 года назад

    its really great video for snippets...thanks sir.....please could provide that XML file where we can paste our code and save as with .snippet extension.

  • @FritzHugo3
    @FritzHugo3 2 года назад

    Why you write this in VSCode and not direct in VS Studio?
    And are a plugin like snippet designer not easier? (dont know wha but the 2022 version are not more so nice like the older 2019 but mybe only a bug).
    realy i would now this, im a total beginner and search for better workflow.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      I addressed that directly at 5:20 in the video.

  • @RobEdwards369
    @RobEdwards369 4 года назад

    Great Video Thankyou. I've made a snippet for a double property. I was wondering if there is an automatic way to change the Capitalisation. For example if I want a private backing field and respect naming conventions.

    • @IAmTimCorey
      @IAmTimCorey  4 года назад

      The snippet should conform to the standard of where it is inserted as far as the layout but naming is still up to the user.

  • @xpl0d3dYT
    @xpl0d3dYT 2 года назад

    11:27 i want to make a c++ snippet and idk how to write the code language, is it CPP or CPlusPlus or what is it?

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      Sorry, I don't know. I'm assuming you can find the answer at docs.microsoft.com

    • @xpl0d3dYT
      @xpl0d3dYT 2 года назад

      @@IAmTimCorey I did! Posted on Stackoverflow, got 4 downvotes and found out it was CPP! Thank's for the template tho

  • @rmxsuga8589
    @rmxsuga8589 2 года назад

    Great! And how do we remove a snippet, in case we decided to change it or not use it in general?
    Tnx for your help! ❤️

    • @v0id_d3m0n
      @v0id_d3m0n Год назад

      I think you can just delete the file

  • @aharonii_
    @aharonii_ 3 года назад

    Hey, can I get help? I want to create a snippet that auto completes methods with braces, instead of pressing tab to auto complete and get `method name`, press tab to auto complete and get `method name()`. It will help me cause most of the time I just need to execute the method. Please help

    • @IAmTimCorey
      @IAmTimCorey  3 года назад +1

      I'm sorry, I don't have time to write code for you. Work the problem through and break it down into small chunks. Work each chunk. You can do it.

    • @aharonii_
      @aharonii_ 3 года назад

      @@IAmTimCorey Thanks for believing in me but I asked for some help not code, do you know how I can edit the snippet that auto completes function names in intelisense?

  • @PaulSpinks
    @PaulSpinks 4 года назад

    I'll use snippets for Dapper Stored Procedures and DataTables from Stored Procedures (SqlDataAdapter).

  • @rong111y
    @rong111y 4 года назад

    A big thank to you Tim, can you also make a tutorial on how to create keyboard shortcuts for like Toggle comment? I personally not a big friend of ctrl + k,c,u... Thanks.

    • @IAmTimCorey
      @IAmTimCorey  4 года назад

      Not really. The problem is that the toggle comments isn't a snippet. It is a reactive system that comments/uncomments a variable number of lines. That is why it is a shortcut, not a snippet. A snippet is a set amount of content that you will insert at a given point. What you can do is change the keyboard shortcut for that event to better fit your process.

  • @YesterdaysStuff
    @YesterdaysStuff 4 года назад

    I made a snippet for an input box and one for logins. I'm always using input boxes.

  • @bonifiedwyrdquietguyesq
    @bonifiedwyrdquietguyesq 3 года назад

    I created a snippet for and Expression Projection pattern

  • @ericklanzas
    @ericklanzas 3 года назад

    Amazing video. Its posible create an ASP NET Snippet??? Its same as html?? Great work!!

  • @anantgoyal9103
    @anantgoyal9103 4 года назад

    Hey Tim! I'm using this to create custom snippets, and even though I've added them in, typing in the shortcut isn't showing the snippet for me. Google isn't helping too much either. Any idea why this could be happening?

    • @IAmTimCorey
      @IAmTimCorey  4 года назад

      Did you add it to the right location in the snippets? Did you give it a shortcut? Can you manually add the snippet?

    • @anantgoyal9103
      @anantgoyal9103 4 года назад

      @@IAmTimCorey I added it as per the directions in the tutorial. It does have a shortcut. Sometimes the shortcut works when I type it, other times it does not.

  • @sneeznoodle
    @sneeznoodle 3 года назад

    Hold up there's a snippet for WriteLine?

  • @linting
    @linting 3 года назад

  • @valeryngwa
    @valeryngwa 4 года назад

    You safe me pains

  • @mistygarrett1290
    @mistygarrett1290 Год назад

    #c

  • @kudorgyozo
    @kudorgyozo 2 года назад

    I expected a 5 minute video max. Do you need a phd to write snippets?. I expect to SAVE TIME by writing snippets and not to watch a 40+ minute video about snippets.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      You don't need a PhD to write a snippet, but you should understand the context around writing a snippet so that you can actually use it in the real world. Context matters in development. Just learning a new trick isn't good enough. You need to know when to use it, when not to use it, how to undo it, and more. Not knowing those things will ensure that you make some bad mistakes as a developer.

  • @JohnNecirRebellion
    @JohnNecirRebellion 4 года назад

    Now I can stop creating methods for methods to simplify/shorten my code.

    • @IAmTimCorey
      @IAmTimCorey  4 года назад

      Great!

    • @JohnNecirRebellion
      @JohnNecirRebellion 4 года назад

      @@IAmTimCorey By the way, I think you forgot to include the Import tag. That tag includes the namespace of the code snippet when it is generated.

  • @Doron_N
    @Doron_N 4 года назад

    or just use jetbrains Rider :)

    • @IAmTimCorey
      @IAmTimCorey  4 года назад +1

      I focus primarily on free solutions because not everyone can afford to pay for a different IDE. Also, with so much built into Visual Studio, I have found that a lot of developers who like alternatives often like them for features they didn't know were also provided for free with Visual Studio.

    • @Doron_N
      @Doron_N 4 года назад

      Yeah, I get that. and yes, all jetbrains software is free for students. I also got a discount once I finished my degree.
      anyway, I find visual studio is very heavy and over complecated, to the point where I prefer to pay for my IDE to do the job.
      I do not work for jetbrains, just a happy customer.
      By the way, nice in depth video. 😊

  • @martintopolsky125
    @martintopolsky125 4 года назад

    Snippets are weak and good for nothing. In 2020 year, it's still not possible to make a primitive snippet like this:
    private $type$ $lowercaseName$;
    public $type$ $uppercaseName$
    {
    get { return $lowercaseName$; }
    set { $lowercaseName$ = value; }
    }

    • @IAmTimCorey
      @IAmTimCorey  4 года назад +1

      That is incorrect. Not only is that possible, I already exists. Type propfull and tab twice. There you go.

    • @martintopolsky125
      @martintopolsky125 4 года назад

      @@IAmTimCorey Thank you for your reply. But 'prop' make only 'public int MyProperty { get; set; }'. My question: is it possible to change a first letter to uppercase from one parameter ? Or is it possible to apply string function to a parameter ? Is it possible to create second parameter from a first one?