Creating Console.ReadLine Snippet in Visual Studio 2019

Unlike cw (for Console.WriteLine) there is no shortcut available for Console.ReadLine in Visual Studio 2019. In this short post I will show you how I’ve created a snippet that binds cr to Console.ReadLine.

1. Save the following as .snippet somewhere on your computer.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>cr</Title>
            <Shortcut>cr</Shortcut>
            <Description>Code snippet for Console.ReadLine</Description>
            <Author>Whoever you want it to be</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal Editable="false">
                    <ID>SystemConsole</ID>
                    <Function>SimpleTypeName(global::System.Console)</Function>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[$SystemConsole$.ReadLine();]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

2. Click on Import… and find the .snippet file you just saved

Importing custom snippet in Visual Studio 2019

3. Click Finish

Creating custom snippet in Visual Studio 2019

Now if you type cr and hit tab twice (just like cw) you’ll have your Console.ReadLine. Cool!

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *