Visual Studio中的十大隱藏寶石-在不增加成本的情況下加快開發速度



Visual Studio不僅僅是文本編輯器。該平台內置有大量電源。在外出並花很多錢購買第三方工具之前,您應該了解Visual Studio中已經可以使用的功能。這就是為什麼在此視頻中,我將向您展示我最喜歡的十項功能,以使您的開發更快,更輕鬆。

完整課程:https://www.iamtimcorey.com/
源代碼:https://leadmagnets.app/?Resource=HiddenGemsInVS
Patreon:https://patreon.com/IAmTimCorey
電子報註冊:https://signup.iamtimcorey.com/

十大清單
1.打開包含文件夾(在選項卡上單擊滑鼠右鍵)並複製完整路徑(在選項卡上單擊滑鼠右鍵)
2.添加有關程序集和NuGet程序包的建議以及首選自動屬性-在「工具」-「選項」-「文本編輯器」-「 C#」-「高級」下
3.轉到定義(F12或Ctrl + Click)或轉到實現(Ctrl + F12)
4.轉到全部(Ctrl + T或Ctrl +,)
5.重構(Ctrl +或Alt + Enter)
6.代碼清理(Ctrl + E,D)-工具-選項-文本編輯器-C#-代碼樣式-格式下的更多清理選項
7.互動式窗口(在「視圖」下-其他Windows-C#互動式)
8.移動代碼(Alt +向上/向下箭頭)
9.顯示Intellisense(Ctrl + J)或顯示參數信息(Ctrl + Shift + Space)
10.多游標(Shift + Alt +上/下箭頭)。

48 comments
  1. I started using tips you provided here. Very helpful ones for me were "go to" search thingie, move class to a new file and remove non used usings on whole project. Btw does useless usings use memory or other resources on production or does it make visual studio faster or is just a cleaner not to have them?

  2. One comment about these tutorials. You could make a simple version control for the examples so that you could more easily show what you changed 🙂

  3. Really helpful been using CTRL + . or ALT + enter but those are really precious gems! Is there a way that when you type the brackets or parenthesis, they always come in pairs. and you have to use your mouse or arrow keys to go the end of the statement just to type semicolon. Really appreciate if you could help. Thank you!

  4. An extension to tip #10, if you hold down CTRL + ALT, you can click anywhere on any line to get the same behavior but it doesn't have to be aligned. Another one i really like is CTRL + D, which will replicate the line of code the cursor is on right below it.

  5. Don't use line number? I use it when we QA or test code by the other developer or a group of developers before UAT. Code is published to server and during testing unhandled or logged exceptions have the line numbers. I go to that branch in source and goto that line number. When dealing with large XML, csv, flat files, JSON and in a hurry I'll open the file with visual studio then use the line number.

    C# interactive is okay, but LINQPad has so much more. It has diff. Dump , UI in html for quick prototype or testing.

  6. For some reason, my my reformat document is Ctrl+K Ctrl+D, I have no memory of remapping it, but I've been using it for ages, and Ctrl+E Ctrl+D does nothing. Odd.

  7. Tips:

    Tip n°1.1 (1:33): Right click on a tab > Open Containing Folder to open File Explorer at the file's location

    Tip n°1.2 (2:19): Right click on a tab > Copy Full Path to copy the full path of the file

    Tip n°2.1 (2:57): Create auto properties when extracting an interface with Quick Actions

    Tip n°2.2 (4:08): Suggest usings for types in reference assemblies and NuGet packages with Quick Actions

    Tip n°3 (8:50): Access implementation using Go To Implementation

    Tip n°4 (11:23): Do a focused search to find types, lines and other useful things using Go To

    Tip n°5 (18:11): Built-in refactoring using Quick Actions

    Tip n°6 (25:12): Easily format using Format Document

    Tip n°7 (27:20): C# Interactive window (Built-in REPL Editor) for quick testing and scripting

    Tip n°8 (31:02): Moving whole lines up and down in the text editor

    Tip n°9 (33:05): Useful IntelliSense shortcuts

    Tip n°10 (35:54): Multi-line modifications

    Shortcuts:

    Quick Actions: (Alt + Enter), (Ctrl + .) or (Ctrl + 😉

    Go To Implementation: (Ctrl + F12) or (Ctrl + Left click)

    Go To: (Ctrl + ,) or (Ctrl + T)

    Format Document: (Ctrl + E, D)

    Moving whole lines: (Alt + Up Arrow) or (Alt + Down Arrow)
    Show IntelliSense: (Ctrl + J)

    Show overloads: (Ctrl + Shift + Space)

    Multi-line modifications: (Shift + Alt + Up Arrow) or (Shift + Alt + Down Arrow)

  8. On to another gem. I'm more of a danosourus, and would assert the reason i like C# as much as I do, things are strongly typed…. So this gem helps out with LINQ…

    Say i have:

    public class foo
    {
    public int val1 { get; set; }
    public int val2 { get; set; }
    public int val3 { get; set; }
    public int val4 { get; set; }
    public int val5 { get; set; }
    }

    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    List<foo> myVars = new List<foo>()
    {
    new foo() {val1 =01, val2 =02, val3=03, val4=04,val5=05},
    new foo() {val1 =11, val2 =12, val3=13, val4=14,val5=15},
    new foo() {val1 =21, val2 =22, val3=23, val4=24,val5=25},
    new foo() {val1 =31, val2 =32, val3=33, val4=34,val5=35},
    };

    var x = myVars.GroupBy( g => new { g.val1, g.val2, g.val3}).Select(g=>g);

    }

    So we now have an anonymous type that will be returned from out linq, but how do we get a strongly typed class, (subtle point it didn't include val4 so i'd assert foo should be used as i might see a lot of lazy people do…) Now move onto the new { and press the key command that is associated to "ShowSmartTag" mine is Shift+Alt+F10, sorry i use an old custom key mapping, not sure of the normal shortcut for others. But once you hit the right key command should see "Convert to file", Convert to Class" and others, but "Convert to Class is what we want. Doing VS will promply you to preview changes, but i've never seen anything meaningful, but go ahaed and click "Apply". First you should see the current line change to:

    var x = myVars.GroupBy( g => new NewClass( g.val1, g.val2, g.val3)).Select(g=>g);

    Notice the new {…} was changed to new NewClass(….).

    Sure enough you get a new strongly typed class…

  9. One more gem i really like. Sometime when debuggering as our objects get populated there might be some specific properties that we are more interested in, say the class's key values or say a count or a datetime value. Let's call these the main values of interest for a given class/object the we want to know say 90% of the time of what makes the object meaningful…

    You may decorate your classes with an attribute to tell Visual Studio what you want to see in the debug watch window. Enough talking… 🙂

    [DebuggerDisplay( "{Property1} {Value2} {Description}" )]
    public class MyClassToShowCustomerDebugInfo

    {
    string Property1 { get ; set }
    int Value2 { get ; set;}
    public MyClass foo { get; set;}
    string Description { get; set; }
    /* More goo… */

    }

    main()
    {
    MyClassToShowCustomerDebugInfo checkThisOut = new …;

    }

    So let's add the variable "checkThisOut" to the debug window.

    Now look at the the variable in the debug watch window., should now see the value for thte instance variable as you specified in the interpolated string you define, Also is did puput in a string label, but you can do that too so "Value2 = {Value2} Desc = {Description"}" the value will be shown as you would expect. Easy and simple and so helpful for the most common cases, should see that you may still drill into the object just as you normally would…

    Enjoy 🙂

  10. My favorite gem not touched, in the article is code snippets. The one i really like if switch.

    When you start a switch for an enum get just:

    switch

    Now press "TAB" (might need to press "ESC" to get rid fo the intellisennse window. But once the code snippet is invoked you enter either an enum or a variable that is an enum type then press enter, should get a case for each value in the enum… Simple example:

    public enum Foo
    {
    One = 1,
    Two = 2,
    Default = 0
    }

    public void Test()
    {
    switch(Foo)
    {
    case Foo.One:
    break;
    case Foo.Two:
    break;
    case Foo.Default:
    break;
    default:
    break;
    }
    }

  11. My goodness Microsoft, you have done some wonderful things here with the VS IDE. But when you have been using the VS IDE for more than two decades to write millions of code to build/refactor/retire dozens of product lines — like Microsoft's Engineers have clearly done — any code editing conveniences sought are inevitably going to make their way into that IDE. And we, outside of Microsoft, benefit from it. And thanks to Tim for pointing these options and conveniences out.

  12. Thanks again for the video Tim. My Resharper trial just expired and I was considering subscribing. However, after watching this video I realized there are several cool features right in Visual Studio that would most likely serve my needs satisfactorily.

    My favourite by far is the interactive window !!!!

  13. Get tips, thanks! When doing the rectangular highlighting, with the mouse, you don't have to hold down the Shift key, just the Alt key.

    In addition to F12 and Ctrl + F2, I love Alt+F2. It shows the definition in a pop up window below where you are editing.

    Also, Ctrl + – is great to use to go back to the previous place(s) you were editing. Ctrl + Shift + – does the reverse (after you have hit Ctrl + -)

  14. Thank you Tim. These cool features should definitely reduce my time going forward. The one which I can use the most would be C# interactive and Shift+Alt+up/down arrow

  15. Ctrl + R + R : Rename
    Rename a variable, property, method, or class. It's easy to remember because of the R (rename).

    Alt + F + F + 1: Open Recent File
    I would have loved an open-recent-file shortcut like VS12's ( Ctrl + Shift + Z ), which is no longer a default shortcut. I have resolved to use Alt + F + F + 1. You can replace the number 1 for any of the files in the menu File / Recent Files. i.e. Alt + F + F + 3 opens the 3rd recent file.

    Tim…thou art the man! Your video tutorials have saved me so much time and, more importantly, sparked curiosity in the topics you cover. Sincerely…Thank you!

  16. FYI, Format Document on VS2019 is Ctrl+K, Ctrl+D. Ctrl+E, Ctrl+D just gives a bell sound. I think this and the refactoring embedded classes to their own file are awesome tips. Thanks Tim!

  17. Great video. I've been using Visual Studio since about the early 1800s, yet I learned several things here that I either didn't know about, or had completely forgotten about. I think it's interesting that Ctrl+K/Ctrl+D and Ctrl+E/Ctrl+D do the same thing.

  18. for me, the F12 and Ctrl E+D is mostly used, and Ctrl+. is really powerful, but i don't know why, the Ctrl+click (go to definition) cmd is not working on my VS (PS i'm using VS2017 15.3.0).

  19. Ctrl + . is really great and powerful. The column selection tip (last one) can also be used just with alt, and can be enhanced by using cut/copy and paste. For example, if you have a in text editor (like Notepad++ that also allows column selection) a list of strings to put in the Console.WriteLine argument, you can copy and paste inside the double quotes and it will fill all the lines, even if your strings have different lengths.

  20. The most satisfying ones are:
    Ctrl+K,C and Ctrl+K,U for commenting and uncommenting big lines of code!

    Write down each one of these then followed by TAB, TAB (these definitely save you big time!): ctor, prop, propg, propfull, for, foreach, try, tryf
    Ctrl+K,X to access all those code snippetsAlt+B+U (Build Project), Alr+B+B (Build solution)
    MS has been extremely consistent on keyboard shortcuts since decades!

  21. These are great tips, but the problem I've always had was that you can get old and gray trying to remember them. If you search for "Cheat Sheet Visual Studio 2017", you'll see a list of the shortcuts. After a while you will remember the ones you use.

  22. Very useful tips thanks, but you missed one very important to me:
    The F2 key.
    It practically does the same as your bonus tip with renaming but not afterwards. You just put your cursor on a variable/method/class … and hit F2 and you can instantly rename it and you get live preview where it will be changed and after hitting enter it will apply.

  23. Try installing CodeMaid extension. Then set is options to auto format and clean your code on save. Use it for years and consider it invaluable and the best extension.

  24. Additional shortcuts I use on daily basis:

    CTRL+M+O = Collapse blocks of code of all members (same as if you collapsed them manually one by one using the [ – ] and [ + ] buttons next to the line number
    CTRL+M+M = Collapse/Expand current block of code
    CTRL+R+R = same as F2 = Refactor identifier under the cursor
    CTRL+Up/DownArrow = Move screen one line up/down (while keeping cursor locked at the current line)
    CTRL+. (dot) on constructor parameter = There is option to "initialize field" – it will create a private member with that type and set it in the body of the constructor… Then you can CTRL+R+R or F2 on the member and rename it to use underscore as prefix
    CTRL+; (semicolon) = jump to Solution explorer (then using down arrow to navigate the listbox) (use ESC to return to the code – not needed to click with mouse)
    CTRL+Tab+Arrows (dont release it) = keeps "switch tab" window open and then using the arrow keys you select which tab/window to switch to
    CTRL+Alt+I = Immediate window – very useful while in break mode when debugging (again, ESCape for return to the code – not needed to click with mouse)
    CTRL+Alt+O = Output window
    CTRL+R+G = removes unnecessary using statements
    CTRL+K+S = Surrounds selected code with ….. (for example, #region, IF statement, etc.)
    CTRL+K+X = pop up "Insert snippet" window

    Snippets (write below text and press Tab twice):
    propf = inserts full property including field
    prop = inserts auto property
    ctor = inserts constructor for current class
    fore = inserts foreach construct
    #reg = inserts #region —- #endregion construct

  25. Great video first of all!
    You've showed a very nice way to clean up the using statements but is there also a quick way to clean the References inside your project?

Comments are closed.