What is Visual Studio .NET snippets? Part one.

    Я решил разбить статью на две части: вступительную часть и часть реализации. Основная причина в размере текста, который включает форматированный текст примеров. С меньшими объемами текста работать легче, да и читать такой текст удобнее. Кто знаком со сниппетами может сразу переходить ко второй части статьи, где рассказывается как сделать свой сниппет и использовать его в студии.

    What are snippets in Visual Studio .NET? As the survey showed, more than a third of voters do not know what it is. It is quite possible, however, that they never worked in this environment and will never do it. 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 template text to the code, for example, defining a property, selecting a region, defining a class. But unlike the primitive shortcut, which would just stick a snippet text blank, it is more convenient to define the code. Snippet has the ability to define the same type of fields in the text, which will be filled with the specified value upon completion of the insertion of snippet (hereinafter “snippet”). I will give an example:

        [global :: System.Serializable] public class MyException: 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 source code was highlighted with Source Code Highlighter .


    Here is an example of the standard snippet'a “exception”, which adds a template to the code to determine the class of the user exception. In the example, the areas of the edited text are highlighted in green, the transition between them occurs by pressing tab. When you change each of them, the corresponding text values ​​in dependent places will change. For example, we name the class NewException and all constructors are instantly renamed. In addition, in some cases, the snippet automatically adds logic that is appropriate in meaning to the code. For example, a snippet for switch and when specifying an enum type in the input field will automatically generate 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.Rcture: 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 snippets? There are two ways to do this: type the known snippet name and press tab or press the 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 the class property.
    Naturally, such a mechanism is extremely useful. It saves time by allowing you to define pre-formatted code with the ability to conveniently customize. Perhaps this will not affect the operation of your program, but it will add some portion of pleasure to you from the coding process. You can get even more benefit by doing your snippets. About this in the second part of the article.

    PS: “there are no errors in the article!” - unfortunately it is impossible to say this, but I will be glad if you pay my attention to the error you found.

    Also popular now: