astype Code Snippet


So, a couple of days ago I discovered just how easy it is to create code snippets for Visual Studio 2005, and this apparently put me into snippet mode. My first attempt at code snippets unintentionally caught a bit of flak, so I hope this second one is less controversial.


Often, I find myself typing code like this:


MyClass mc = foo as MyClass;
if (mc != null)
{
    // Do something meaningful with mc here
}

It’s not difficult, but when you’ve done it a couple of hundred times, it gets a bit tedious to type, so here’s the astype code snippet:


<?xml version=1.0 encoding=utf-8 ?>
<CodeSnippets  xmlns=http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet>
    <CodeSnippet Format=1.0.0>
        <Header>
            <Title>astype</Title>
            <Shortcut>astype</Shortcut>
            <Description>Code snippet for casting an object to a type and working on the casted instance.</Description>
            <Author>Mark Seemann</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>type</ID>
                    <ToolTip>The destination type</ToolTip>
                    <Default>object</Default>
                </Literal>
                <Literal>
                    <ID>variable</ID>
                    <ToolTip>The name of the new variable.</ToolTip>
                    <Default>name</Default>
                </Literal>
                <Literal>
                    <ID>expressionToCast</ID>
                    <ToolTip>The expression to cast.</ToolTip>
                    <Default>expression</Default>
                </Literal>
            </Declarations>
            <Code Language=csharp><![CDATA[$type$ $variable$ = $expressionToCast$ as $type$;
    if ($variable$ != null)
    {
    }]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Again, just paste this XML into a text file and save it to the C:\Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033\Visual C# and begin using it by typing astype in the code editor.


If you don’t care for the copying and pasting, I’ve also included the snippet file as an attachment to this post, so you can just dowload it to your C:\Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033\Visual C# folder and start using it right away.

astype.snippet

Comments (4)

  1. Yatharth Gupta says:

    I am a big fan of snippets. It improves my productivity, esp while coding APIs,

    You have to check this out ..

    http://www.gotdotnet.com/codegallery/codegallery.aspx?id=b0813ae7-466a-43c2-b2ad-f87e4ee6bc39

    Program to easily create snippets.

  2. ploeh says:

    Yatharth, thank you for the tip!

  3. George says:

    I understand this post is old, but I'll just leave this here:

    if (foo is MyClass)

    {

       //Do something meaninful here.

    }

  4. ploeh says:

    George, thank you for your comment. It's true that you can use the 'is' operator if all you need to do is check for the type, but that requires that the code block indicated by 'Do something meaningful here' doesn't need an instance of MyClass, but can operate on the less specific 'foo' instance.

    On the other hand, if you need MyClass in the code block, with the 'is' keyword you would have to cast the 'foo' instance to MyClass. However, 'as' and 'is' are both casts in their own right, so that would involve a double cast, which is expensive. The 'as' keyword followed by a null check lets you do it with only one cast.

    However, there are cases (e.g. with value types and unconstrained generics) where this is sometimes the only possible thing to do. It is, however, so rare that I didn't find it worth a Code Snippet.