Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change default behavior of prop shortcut in Visual Studio

When we use the prop shortcut to generate a property, it generates it as an int. However, in most of the cases, I use string properties. Is there any way I can change the default behavior of this shortcut to generate string properties?

like image 299
Katana Avatar asked Dec 23 '22 10:12

Katana


1 Answers

I wrote my own snippets for custom behavior like that. like props for string etc.

If you open up Code Snippet Manager in Visual studio it will show you the location of the snippet file, which is editable. The location varies based on the version if VS you are using.

On VS2013 it showed the following path for prop for c#

C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC#\Snippets\1033\Visual C#/prop.snippet

I am then able to open it a text editor and customize the file as needed.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>prop</Title>
            <Shortcut>prop</Shortcut>
            <Description>Code snippet for an automatically implemented property
Language Version: C# 3.0 or higher</Description>
            <Author>Microsoft Corporation</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>type</ID>
                    <ToolTip>Property type</ToolTip>
                    <Default>string</Default>
                </Literal>
                <Literal>
                    <ID>property</ID>
                    <ToolTip>Property name</ToolTip>
                    <Default>MyProperty</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[public $type$ $property$ { get; set; }$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Note how the above was changed to string for default type

like image 121
Nkosi Avatar answered May 14 '23 07:05

Nkosi