[VS] Hướng Dẫn Tạo Code Snippet Trong Visual Studio - C# Căn Bản

Chia sẻ

Sử dụng Code Snippet trong Visual Studio giúp giảm số lần gõ phím, tăng tốc độ coding (giảm thời gian coding) cũng tức là giảm bệnh nghề nghiệp, tăng thêm thời gian để tập trung vào việc hữu ích hơn (so với coding).

Nội dung

Một số Code Snippet C# có sẵn

prop – tạo thuộc tính tự thực hiện (auto-implemented property). propfull – tạo thuộc tính có đủ get set. propg – tạo thuộc tính tự thực hiện chỉ đọc. else, if, do, for, foreach, switch, while – tạo các câu lệnh tương ứng struct, class, enum – khai báo các kiểu try, tryf – tạo khối try, try-finally

Nơi lưu trữ Code Snippet

Nơi lưu trữ Code Snippet có sẵn nằm trong thư mục: “C:Program FilesMicrosoft Visual Studio 12.0VC#Snippets1033Visual C#”

Các Code Snippet do ta tự tạo sẽ nằm trong thư mục “C:UsersTênUserDocumentsVisual Studio 2013Code SnippetsVisual C#My Code Snippets” (Win 8)

Tạo một Code Snippet

Sau đây ta sẽ tự tạo một Code Snippet đối với khối cấu trúc try…catch. Theo đoạn mã như sau.

<?xml version="1.0" encoding="utf-8" ?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Title>try</Title> <Shortcut>try</Shortcut> <Description>Code snippet for try catch</Description> <Author>Microsoft Corporation</Author> <SnippetTypes> <SnippetType>Expansion</SnippetType> <SnippetType>SurroundsWith</SnippetType> </SnippetTypes> </Header> <Snippet> <Declarations> <Literal> <ID>expression</ID> <ToolTip>Exception type</ToolTip> <Function>SimpleTypeName(global::System.Exception)</Function> </Literal> </Declarations> <Code Language="csharp"><![CDATA[try { $selected$ } catch ($expression$ ex) { MessageBox.Show(ex.ToString()); }]]> </Code> </Snippet> </CodeSnippet> </CodeSnippets>

Kết quả

ĐĂNG KÝ MUA HÀNG

Email (*) Điện thoại (*) Tên sản phẩm/Dịch vụ:

Δ

Lượt xem: 2.701 Chia sẻ Xem thêm [WPF] Hướng dẫn sử dụng Styling cho Button trong wpf

Điều hướng bài viết

[C#,WPF] Tạo ứng dụng chạy với quyền quản trị [WPF] Hướng dẫn tạo splash screen trong WPF

Từ khóa » Vs 2019 Custom Snippets