5 Useful Visual Studio C# Snippets – Part 2
- 11
- Add a Comment
If you're new here, you may want to subscribe to my RSS feed or become a fan of this blog on Facebook. Thanks for visiting!
I initially published 5 Visual Studio c# Snippets yesterday and it got a lot of attention and had some positive feedback, so this is a follow up that I hope would be as helpful as the first one.
Reading an embedded resource as a stream
Specially when sending patches to customers, sometimes I need to embed an XML file that would contain some data that needs to be imported to the database in a compiled exe or dll to simplify the process for them. So I ended up creating a snippet on how to read an embedded resource
Shortcut: rembed
Sample Result:
Stream rulesReader = typeof(MyObject).Assembly.GetManifestResourceStream(
typeof(MyObject), "Resources.AttestationContratWorkflow.rules");
Customization:
- The type of the object to get the assembly from
- The name of the embedded resource
ViewState property
Even though I avoid using the viewstate as much as I can to keep the size of the page small but sometimes you really need to.
Shortcut: vstate
Sample Result:
public string Title { get { if (ViewState["Title"] == null) return "My Title"; return (string)ViewState["Title"]; } set { ViewState["Title"] = value; } }
Customization:
- The type of the property
- The name of the property
- The default return value
Null argument validation
I would say this is one of the basic best practices to have and to stick to. My Eiffel professor would be so proud of me
Shortcut: nparam
Sample Result:
if (context == null) { throw new ArgumentNullException("context "); }
Customization:
- The name of the parameter
Info Message Box
This snippets shows an Information Message box.
Shortcut: ibox
Sample Result:
MessageBox.Show(this, "My Information Message", "Information...", MessageBoxButtons.OK,
MessageBoxIcon.Information);
Customization:
- The message
Error Message Box
Same as the previous snippet but shows an error message instead
Shortcut: ebox
Sample Result:
MessageBox.Show(this, "My Error Message", "Error...", MessageBoxButtons.OK,
MessageBoxIcon.Error);
Customization:
- The message
Hope this helps!
Hatim
11 Comments
Rene Schulte
April 16th, 2009
at 1:10pm
“Null argument validation” Snippet:
The quotes for the ArgumentNullException parameter are missing.
Instead of:
throw new ArgumentNullException($paramName$);
it should be:
throw new ArgumentNullException(”$paramName$”);
Hatim
April 16th, 2009
at 2:28pm
Rene,
Thanks for catching this It’s fixed now!
Sam
April 16th, 2009
at 6:06pm
public string Title
{
get
{
return (string)ViewState["Title"] ?? “My Title”;
}
set
{
ViewState["Title"] = value;
}
}
Jerome Laban
April 16th, 2009
at 7:03pm
About the ArgumentNullException snippet, I find myself using the Umbrella Library (http://www.codeplex.com/umbrella), where one can write something like this:
context.Validation().NotNull(”context”);
This way, I’m sure I get the code right, and I get a slightly better code coverage because the library has already been tested.
This is no silver bullet, but I find it easier to read.
Stuart Clark
April 16th, 2009
at 10:49pm
Your embedded resource example to embed an xml file, If your project is .Net 2.0 + its much easier to use a resource file to achieve this.
Valamas
April 17th, 2009
at 2:11am
I cannot see Tools –> Code Snippets Manager in my Visual Studio 2005 / 2008. Can anyone help?
Fredrik von Walden
April 17th, 2009
at 5:45am
Valmas:
Perhaps you could try the keyboard shortcut. Ctrl+K, Ctrl+B
mojojojo
April 17th, 2009
at 6:54am
You can try CTRL+K, CTRL+B hotkey combo
Reflective Perspective - Chris Alcock » The Morning Brew #329
April 17th, 2009
at 7:31am
[...] 5 Useful Visual Studio C# Snippets – Part 2 - Hatim Rih shares a few more Code Snippets [...]
Gerardo Contijoch
April 17th, 2009
at 10:24pm
Null Argument Validation snippet is one of the most useful pieces of code I’ve seen!!
I love it!
Why didn’t I come with something like that before?
Valamas
April 20th, 2009
at 5:17am
CTRL+K, CTRL+B works! thank you so much