1. Các hotkey cơ bản
Sử dụng hotkey nhiều sẽ khiến người ngoài thấy bạn code có vẻ prohơn. Bạn cũng đỡ tốn công di chuyển chuột hơn
F5 – Ctrl F5 – Debug
Sau khi code xong, thay vì phải di chuyển chuột lên, bấm nút Start này để chạy chương trình, hãy bấm F5 (Cái này cơ bản quá rồi, bỏ vào cho những ai chưa biết thôi).
Trường hợp ta đặt breakpoint để bug, hãy bấm Ctrl + F5 để chạy chế độ no debug, bỏ qua breakpoint nhé.
F12 – See implementation
Khi đọc code, ta muốn xem 1 class hay method viết như thế nào, hãy sử dụng phím F12. Nếu bạn cài Reshaper hoặc VS Productivity Tool, bạn có thể giữ Ctrl và click vào class để có hiệu quả tương tự
Ctrl K + Ctrl D – Format Code
Code xong, nếu thấy code xiêu vẹo thì hãy dùng Ctrl K + Ctrl D để tự động format lại. Nếu code không tự động format thì code thể là bạn quên đóng () hoặc {} nhé.
Ctrl K + Ctrl C – Comment Code / Ctrl K + Ctril U – Uncomment Code
Để comment code, ta thường comment bằng tay dùng 2 nút này trên toolbar
Ta có thể áp dụng hotkey để comment/uncomment nhanh nhé
F9 – Set breakpoint
Để đặt breakpoint ta thường phải click vào thanh dọc bên trái. Sao phải làm vậy khi ta có thể bấm F9 là xong, nhanh gọn lẹ.
F10/F11 – Step over/Step into
Khi debug, thay vì bấm 2 nút trên toolbar này, dùng hotkey F10 và F11 sẽ tiết kiệm thời gian hơn.
2. Sử dụng snippet
Trong quá trình code, có những đoạn code ta phải lặp lại nhiều lần. Visual Studio đã tích hợp sẵn những đoạn code này, gọi là snippet, tiết kiệm khá nhiều thời gian (Netbeans hay Eclipse cũng có).
Cách sử dụng snippet: Gõ ra rồi Tab là xong
ctor – Tạo contrucstor cho class
prop – Tạo properties – Gõ xong nhớ Tab Tab
Hồi mới chuyển từ java qua C#, mình thắc mắc sao Visual Studio xịn vậy mà ko có cái tạo getter, setter cho field. Sau này mới biết getter, setter được hiện thực bằng property nhé.
cw – Console.WriteLine() – Cho mấy bạn hay code Console
for/forr – Tự tạo hàm for
Các bạn có thể thử với một số snippet khác như if/else, do/while, switch, try nhé.
3. Một số mẹo nhỏ khác
Giữ Ctrl + Nhấp chuột – Box Selection
Bạn từng gặp cảnh phải sửa nhiều dòng code 1 lúc chưa? Sử dụng Box Selection sẽ giúp việc này trở nên vô cùng dễ dàng
Ctrl + Shift + V – Nghịch clipboard
Trong đời code, chắc ai cũng đã từng phải copy paste. Đã bao giờ bạn từng copy A, sau đó lỡ bấm nhầm copy B, C, sau đó phải tìm A để copy lại chưa? Visual Studio lưu lại tận 20 lần copy gần nhất trong clipboard, ta có thể dùng Ctrl + Shift + V để tìm đoạn clipboard cần paste.
Ctrl + , – Navigate File/Ctrl + ; : Search
Thay vì Ctrl + F, hãy sử dụng 2 chức năng này, hữu hiệu hơn nhiều đấy
Paste JSON/XML as class
Mình không biết chức năng này được Microsoft thêm vào Visual Studio từ khi nào, nhưng mà nó khá hữu dụng. Hiện nay, khi nhà nhà dùng Rest API, người người dùng Rest API, JSON và XML trở thành hotboy hotgirl. Trong C#, ta không thể dùng thẳng mà phải convert JSON sang object. Chức năng này cho phép ta tạo class để convert json sang.
VD mình có chuỗi JSON sau (Lấy từ facebook graph api):
1
2
3
4
5
6
7
8
9
10
11
12
| { "id": "1000017928484843", "first_name": "Mạnh Chức", "gender": "male", "last_name": "Lê", "locale": "vi_VN", "name": "Lê Mạnh Chức", "timezone": 7, "updated_time": "2017-02-06T12:56:23+0000", "verified": true} |
Chỉ cần dùng paste JSON as class, dán đoạn JSON này vào, sau đó chọn uppercase các kí tự đầu tiên của các field là xong. VS khá thông minh, có thể xác định một số trường là datetime hoặc boolean, còn mặc định là string.
Bài viết load hơi nặng vì có nhiều ảnh động, các bạn thông cảm. Chúc các bạn ngày càng thành thục với IDE của mình. Mấy trò hotkey snippet v…v này rất phù hợp để “chứng tỏ trình độ” hoặc “hù mem mới” nhé.
1. Basic hotkeys
Using hotkeys a lot makes your coding look moreproto others. You also move the mouse a lot less
F5 - Ctrl F5- Debug
When you finish coding, instead of moving the mouse up and clicking the Start button to run the program, just press F5 (very basic, added for anyone who does not know it yet).
If you set breakpoints to debug, press Ctrl + F5 to run without debugging and skip the breakpoints.
F12 - See implementation
When reading code and you want to see how a class or method is written, press F12. If you have ReSharper or VS Productivity Tool installed, you can also hold Ctrl and click the class for the same result
Ctrl K + Ctrl D - Format Code
When the code looks messy, use Ctrl K + Ctrl D to format it automatically. If it does not format, you probably forgot to close a () or {}.
Ctrl K + Ctrl C - Comment Code /Ctrl K + Ctrl U - Uncomment Code
To comment code, we usually do it by hand with these 2 buttons on the toolbar
Use the hotkeys to comment and uncomment quickly
F9 - Set breakpoint
To set a breakpoint you usually click the vertical bar on the left. Why do that when pressing F9 is quicker?
F10/F11 - Step over/Step into
When debugging, using F10 and F11 instead of these 2 toolbar buttons saves time.
2. Use snippets
While coding, some pieces of code have to be written again and again. Visual Studio has them built in as snippets, which saves a lot of time (NetBeans and Eclipse have them too).
How to use a snippet: Type it, then pressTab and you are done
ctor - Create a constructor for the class
prop - Create a property. Press Tab Tab after typing
When I moved from Java to C#, I wondered why such a good IDE as Visual Studio had no generator for the getter and setter of a field. Later I learned that in C# getters and setters are written as aproperty instead.
cw - Console.WriteLine(), for those who write console apps a lot
for/forr - Create a for loop
You can also try other snippets likeif/else, do/while, switch, try too.
3. Other small tips
Hold Ctrl + click- Box Selection
Ever had to edit many lines at once? Box Selection makes this very easy
Ctrl + Shift + V - Clipboard ring
Everyone who codes copies and pastes. Have you ever copied A, then by mistake copied B and C, and had to find A to copy it again? Visual Studio keeps the last 20 copies in its clipboard, and Ctrl + Shift + V lets you pick the one you want to paste.
Ctrl + , - Navigate File/Ctrl + ; : Search
Instead of Ctrl + F, use these 2 features, they work much better
Paste JSON/XML as class
I do not know when Microsoft added this feature to Visual Studio, but it is quite useful. Now that every project uses REST APIs, JSON and XML are everywhere. In C# you cannot use JSON directly, you have to convert it to an object. This feature creates the class to convert the JSON into.
For example, I have this JSON string (from the Facebook Graph API):
Just use Paste JSON as class, paste the JSON in, then make the first letter of each field uppercase and you are done. VS is quite smart: it detects some fields as datetime or boolean, and everything else defaults to string.
This post loads a bit slowly because of the many animated images, sorry about that. I hope you get better and better with your IDE. These hotkey and snippet tricks are great for “showing off your skills” or “impressing new teammates”.
1. Grundlegende Tastenkürzel
Wer viele Tastenkürzel nutzt, wirkt beim Programmierenprofessionellerals andere. Außerdem bewegst du die Maus viel seltener
F5 - Ctrl F5- Debug
Wenn du fertig bist, drück einfach F5, statt mit der Maus nach oben zu fahren und auf Start zu klicken (sehr einfach, nur für alle, die es noch nicht kennen).
Wenn du Breakpoints zum Debuggen gesetzt hast, starte mit Ctrl + F5 ohne Debugger und überspringe die Breakpoints.
F12 - Implementierung ansehen
Wenn du beim Lesen sehen willst, wie eine Klasse oder Methode geschrieben ist, drück F12. Mit ReSharper oder VS Productivity Tool kannst du auch Ctrl gedrückt halten und auf die Klasse klicken
Ctrl K + Ctrl D - Code formatieren
Sieht der Code unordentlich aus, formatiert Ctrl K + Ctrl D ihn automatisch. Klappt das nicht, fehlt wahrscheinlich eine schließende () oder {}.
Ctrl K + Ctrl C - Code auskommentieren /Ctrl K + Ctrl U - Kommentar entfernen
Zum Auskommentieren klickt man meist von Hand auf diese 2 Schaltflächen in der Symbolleiste
Mit den Tastenkürzeln geht das Ein- und Auskommentieren viel schneller
F9 - Breakpoint setzen
Für einen Breakpoint klickt man meist auf die senkrechte Leiste links. Mit F9 geht es schneller.
F10/F11 - Step over/Step into
Beim Debuggen sparen F10 und F11 Zeit gegenüber diesen 2 Schaltflächen in der Symbolleiste.
2. Snippets nutzen
Manche Codestücke schreibt man immer wieder. Visual Studio bringt sie als Snippets mit und spart damit viel Zeit (NetBeans und Eclipse haben das auch).
So nutzt du ein Snippet: Eintippen, dannTab drücken, fertig
ctor - Konstruktor für die Klasse anlegen
prop - Property anlegen, danach Tab Tab drücken
Als ich von Java zu C# wechselte, wunderte ich mich, warum ein so gutes Visual Studio keine Getter und Setter für Felder erzeugt. Später lernte ich, dass Getter und Setter in C# alsproperty geschrieben werden.
cw - Console.WriteLine(), für alle, die viel mit der Konsole arbeiten
for/forr - for-Schleife anlegen
Probier auch andere Snippets aus, zum Beispielif/else, do/while, switch, try und mehr.
3. Weitere kleine Tipps
Ctrl gedrückt halten + klicken- Box Selection
Musstest du schon einmal viele Zeilen auf einmal ändern? Mit Box Selection geht das ganz leicht
Ctrl + Shift + V - Zwischenablage-Ring
Jeder, der programmiert, kopiert und fügt ein. Hast du schon einmal A kopiert, dann aus Versehen B und C kopiert und A wieder suchen müssen? Visual Studio merkt sich die letzten 20 Kopien, und mit Ctrl + Shift + V wählst du die gewünschte aus.
Ctrl + , - Datei finden/Ctrl + ; : Suche
Statt Ctrl + F lieber diese 2 Funktionen nutzen, sie sind viel hilfreicher
Paste JSON/XML as class
Ich weiß nicht, seit wann Microsoft diese Funktion in Visual Studio hat, aber sie ist sehr praktisch. Heute nutzt jedes Projekt REST-APIs, JSON und XML sind überall. In C# kann man JSON nicht direkt verwenden, sondern muss es in ein Objekt umwandeln. Diese Funktion erzeugt die passende Klasse dafür.
Zum Beispiel habe ich diesen JSON-String (aus der Facebook Graph API):
Einfach Paste JSON as class wählen, das JSON einfügen und den ersten Buchstaben jedes Felds großschreiben, fertig. VS ist ziemlich schlau: Manche Felder erkennt es als datetime oder boolean, alles andere wird string.
Der Beitrag lädt wegen der vielen animierten Bilder etwas langsam, sorry. Viel Erfolg mit deiner IDE. Diese Tastenkürzel und Snippets eignen sich bestens, um „Können zu zeigen“ oder „neue Kollegen zu beeindrucken“.
















0 nhận xét:
Đăng nhận xét