: . , . , . , .
What are snippet's in Visual Studio .NET? According to the survey, more than a third of those who voted do not know what it is. It is possible, however, that they have never worked in this environment and will never do that. This article is for those twenty percent who are interested and for everyone else who wants to know what snippet is, how to create and use it.
In fact, everything is simple: snippet is a mechanism that allows you to quickly add some kind of template text to the code, for example, property definition, region selection, class definition. But unlike the primitive shortcut, which simply sticks textual snippet, it is more convenient to determine the code. Snippet has the ability to define fields of the same type in the text, which will be filled with the specified value upon completion of the snippet insert (hereinafter referred to as the “snippet”). I will give an example:
[global :: System. Serializable ] public class My Exception: Exception
{ public MyException () {} public MyException ( string message): base (message) {} public MyException ( string message, Exception inner): base (message, inner) {} protected MyException (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context): base (info, context) {}} This is a code highlighted with Source Code Highlighter .
Here is an example of a standard snippet "a exception", which adds a pattern to the code to define a custom exception class. The example highlighted in green areas of the editable text, the transition between them occurs by pressing the tab. If you change each of them, the corresponding text values ​​in the dependent places will change. For example, name the class NewException and all constructors are instantly renamed. In addition, in some cases, the snippet automatically adds logic to the code that matches the meaning. For example, a snippet for a switch and if you specify an instance of the enum type in the input field, it will automatically form a case for each enum element and add default:
public enum USER_TYPE {UNKNOWN = 0, REGISTERED, NCF_CLERK, CLERK};
...
switch (p_uType) { case CommonUtil.USER_TYPE.UNKNOWN: break ; case CommonUtil. USER_TYPE. Reverse: break ; case CommonUtil.USER_TYPE.NCF_CLERK: break ; case CommonUtil.USER_TYPE.CLERK: break ; default : break ; } * This source code was highlighted with Source Code Highlighter .
')
How to work with snippet? There are two ways to do this: type the well-known name of the snippet and press tab or press shortcut ctrl + k + x to display a list of all registered snippets. The first option is more convenient: you can type, for example, “prop”, press tab and instantly get a template definition of a class property.
Naturally, such a mechanism is extremely useful. It saves time by allowing you to define pre-formatted code with convenient customization. Perhaps this will not affect the operation of your program in any way, but it will add to you a certain portion of the pleasure from the coding process. Even more benefits can be obtained by making your snippets. About this in the second part of the article.
PS: “there are no errors in the article!” - unfortunately, this is impossible to say, but I will be glad if you pay my attention to the error you found.