A snippet for handling each native type in C# and Visual Studio…when generics won't work.
So sometimes you have to have a function that can do something to any native type.
Below are two snippets to speed up the coding for you.
Ok, so of course there are times when you can use Generics.
public class myclass<t> { myclass(T inT) { // your class } }
However, there are functions that don’t work with a generic type T.
public class myclass<t> { myclass(T inT1, T inT2) { if (inT1 < inT2) { // do something } } } [/sourcecode] This results in an error: Error 1 Operator '<' cannot be applied to operands of type 'T' and 'T'. Maybe you need to handle all the native data types. So it is annoying to type them in, so I created an iftype snippet. Create a file called iftype.snippet in this directory: C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC#\Snippets\1033\Visual C#\ Copy in this source and save the file. [sourcecode language="csharp"] <?xml version="1.0" encoding="utf-8" ?> <codeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <codeSnippet Format="1.0.0"> <header> <title>iftype</title> <shortcut>iftype</shortcut> <description>Code snippet for an automatically implemented an 'if' statement for each native type.</description> <author>Jared Barneck</author> <snippetTypes> <snippetType>Expansion</snippetType> </snippetTypes> </header> <snippet> <declarations> <literal> <id>varName</id> <toolTip>Variable name</toolTip> <default>t</default> </literal> </declarations> <code Language="csharp"><![CDATA[ if ($varName$.Equals(typeof(bool))) { throw new NotImplementedException(); } else if ($varName$.Equals(typeof(Byte))) { throw new NotImplementedException(); } else if ($varName$.Equals(typeof(Char))) { throw new NotImplementedException(); } else if ($varName$.Equals(typeof(DateTime))) { throw new NotImplementedException(); } else if ($varName$.Equals(typeof(Decimal))) { throw new NotImplementedException(); } else if ($varName$.Equals(typeof(Double))) { throw new NotImplementedException(); } else if ($varName$.Equals(typeof(Int16))) { throw new NotImplementedException(); } else if ($varName$.Equals(typeof(Int32))) { throw new NotImplementedException(); } else if ($varName$.Equals(typeof(Int64))) { throw new NotImplementedException(); } else if ($varName$.Equals(typeof(SByte))) { throw new NotImplementedException(); } else if ($varName$.Equals(typeof(Single))) { throw new NotImplementedException(); } else if ($varName$.Equals(typeof(String))) { throw new NotImplementedException(); } else if ($varName$.Equals(typeof(TimeSpan))) { throw new NotImplementedException(); } else if ($varName$.Equals(typeof(UInt16))) { throw new NotImplementedException(); } else if ($varName$.Equals(typeof(UInt32))) { throw new NotImplementedException(); } else if ($varName$.Equals(typeof(UInt64))) { throw new NotImplementedException(); } $end$]]> </code> </snippet> </codeSnippet> </codeSnippets>
However, you prefer a switch statement to an if statement. Here is the same thing using the switch statement.
Create a file called switchtype.snippet in this directory: C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC#\Snippets\1033\Visual C#\
Copy in this source and save the file.
<?xml version="1.0" encoding="utf-8" ?> <codeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <codeSnippet Format="1.0.0"> <header> <title>switchtype</title> <shortcut>switchtype</shortcut> <description>Code snippet for an automatically implemented a switch statement for each native type.</description> <author>Jared Barneck</author> <snippetTypes> <snippetType>Expansion</snippetType> </snippetTypes> </header> <snippet>a <declarations> <literal> <id>varName</id> <toolTip>Variable name</toolTip> <default>varName</default> </literal> </declarations> <code Language="csharp"><![CDATA[ Type t = $varName$; switch (t.ToString()) { case "System.Boolean": throw new NotImplementedException(); break; case "System.Byte": throw new NotImplementedException(); break; case "System.Char": throw new NotImplementedException(); break; case "System.DateTime": throw new NotImplementedException(); break; case "System.Decimal": throw new NotImplementedException(); break; case "System.Double": throw new NotImplementedException(); break; case "System.Int16": throw new NotImplementedException(); break; case "System.Int32": throw new NotImplementedException(); break; case "System.Int64": throw new NotImplementedException(); break; case "System.SByte": throw new NotImplementedException(); break; case "System.Single": throw new NotImplementedException(); break; case "System.String": throw new NotImplementedException(); break; case "System.TimeSpan": throw new NotImplementedException(); break; case "System.UInt16": throw new NotImplementedException(); break; case "System.UInt32": throw new NotImplementedException(); break; case "System.UInt64": throw new NotImplementedException(); break; } $end$]]> </code> </snippet> </codeSnippet> </codeSnippets>