5 Useful Visual Studio C# Snippets – Part 2

Trusted By

Welcome Back! I hope you enjoy the content on this site. If you have not done so already, 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

 

Download the snippets

Hope  this helps!

Hatim

Digg This

11 Comments

“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$”);

Rene,
Thanks for catching this It’s fixed now!

public string Title
{
get
{
return (string)ViewState["Title"] ?? “My Title”;
}
set
{
ViewState["Title"] = value;
}
}

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.

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.

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

You can try CTRL+K, CTRL+B hotkey combo

[...] 5 Useful Visual Studio C# Snippets – Part 2 - Hatim Rih shares a few more Code Snippets [...]

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?

CTRL+K, CTRL+B works! thank you so much

Leave a Comment