Udostępnij za pośrednictwem


Code Snippet for ArgumentNullException

I got a chance to see BradA give his talk at TriNUG Wednesday night, and in honor of that (specifically, the part where he explains why an API may do a disservice to its consumers if it needs an argument to be non-null and just lets null refs happen instead of argument checking) is a quickie snippet for ArgumentNullException throwing on null parameters.

It's obviously not ground-breaking stuff, but hopefully the easier that adding the checking becomes on developers, the more likely it is they'll provide it, right? :)

<?xml version="1.0" encoding="utf-8" ?>

<CodeSnippets  xmlns="https://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

  <CodeSnippet Format="1.0.0">

    <Header>

      <Title>argument null check</Title>

      <Shortcut>an</Shortcut>

      <Description>Code snippet for checking a parameter for null and throwing an ArgumentNullException</Description>

      <Author>Microsoft Corporation</Author>

      <SnippetTypes>

        <SnippetType>Expansion</SnippetType>

      </SnippetTypes>

    </Header>

    <Snippet>

      <Declarations>

        <Literal>

          <ID>parametername</ID>

          <ToolTip>Parameter to do the null check for</ToolTip>

          <Default>parameterToCheck</Default>

        </Literal>

      </Declarations>

      <Code Language="csharp">

        <![CDATA[if ($parametername$ == null)

        {

            throw new ArgumentNullException("$parametername$");

        }

        $end$]]>

      </Code>

    </Snippet>

  </CodeSnippet>

</CodeSnippets>