3

I'm often writing a c# code that goes like this:

    void func(object myObject)
    {
        if (myObject == null)
        {
            throw new ArgumentNullException("myObject");
        }
        ...

How do I write an auto-complete in Visual Studio 2012 for that type of code, so that I don't have to keep typing this all the time?

galets
  • 17,802
  • 19
  • 72
  • 101
  • 1
    There is a nice and free add-in called [Snippet Designer](http://snippetdesigner.codeplex.com/) Downloadable also from Visual Studio Gallery – Steve Sep 19 '13 at 21:54

3 Answers3

9

Since there hasn't been an interest, I'm going to close on this question by posting my own solution. Putting following file into C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC#\Snippets\1033\Visual C#\ifn.snippet will do the trick:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
        <Title>ifn</Title>
        <Shortcut>ifn</Shortcut>
        <Description>Code snippet for if (arg==null)</Description>
        <Author>sam green</Author>
        <SnippetTypes>
            <SnippetType>Expansion</SnippetType>
            <SnippetType>SurroundsWith</SnippetType>
        </SnippetTypes>
    </Header>
    <Snippet>
        <Declarations>
            <Literal>
                <ID>argument</ID>
                <Default>arg</Default>
                <ToolTip>Argument</ToolTip>
            </Literal>
        </Declarations>
        <Code Language="csharp"><![CDATA[if ($argument$ == null)
        {
                    throw new ArgumentException("$argument$");
        }$end$]]>
        </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
galets
  • 17,802
  • 19
  • 72
  • 101
1

I defined a ReSharper shortcut for this using the keywords "argnull" that auto-generates and guesses the parameter I want. It's about the same effect as your solution, I think, though you don't need to muck with XML or where to place files.

Here's an example of a ReSharper VB.NET approach:

if $VAR$ is nothing then
  throw new ArgumentNullException("$VAR$")
end if
Matt Eland
  • 348
  • 1
  • 4
  • 13
  • I would recommend you use `NameOf`, that is: `throw new ArgumentNullException(NameOf($VAR$"))` – Ama Feb 06 '20 at 18:51
0

To improve on @galet's answer, here's mine:

  • Uses a succinct single-line if() throw; statement.
  • Uses the more precise ArgumentNullException instead of ArgumentException.
  • Uses nameof() to avoid magic-strings (and so will work perfectly with Refactor-Renames).
  • Uses the (safer) is null operator:
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
        <Title>ifn</Title>
        <Shortcut>ifn</Shortcut>
        <Description>Code snippet for if (arg==null)</Description>
        <Author>https://stackoverflow.com/questions/18905475</Author>
        <SnippetTypes>
            <SnippetType>Expansion</SnippetType>
            <SnippetType>SurroundsWith</SnippetType>
        </SnippetTypes>
    </Header>
    <Snippet>
        <Declarations>
            <Literal>
                <ID>argument</ID>
                <Default>arg</Default>
                <ToolTip>Argument</ToolTip>
            </Literal>
        </Declarations>
        <Code Language="csharp"><![CDATA[if( $argument$ is null ) throw new ArgumentNullException( nameof($argument$) );$end$]]>
        </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
Dai
  • 141,631
  • 28
  • 261
  • 374