views:

902

answers:

8

What are the best code snippets for C#? (using visual studio) VB has a lot that are pre-defined, but there are only a handful for C#. Do you have any really useful ones for C#?

Anyone want to post a good custom one you created yourself?

Anyone?... Bueller?

+1  A: 

prop and exception are my favorites.

Martin Clarke
+4  A: 

There's plenty of code snippets within Visual Studio for basic programming structure but I wouldn't necessarily rate one higher than another.

I would definitely say the best ones are the custom snippets you define yourself to accomplish more specific tasks that you may find yourself using on a regular basis. Definitely a big time saver.

A fairly basic intro to creating custom snippets can be found at http://www.15seconds.com/issue/080724.htm to help with this.

Shane Kenney
+1  A: 

These are the ones I use daily.

  • prop
  • try
  • if
  • else
  • for
  • foreach
  • mbox - Message box stub
  • The ability to role your own. I have one for Property that are saved in the view state, methods a custom class example.
David Basarab
A: 

I just started a blog, where I document short solutions in C# (code snippets) that I came up with and might prove useful to other coders.

http://thorstenlorenz.blogspot.com/

So far I have mostly blogged about extension methods and generics.

So have a look and tell me what you think.

Thorsten Lorenz
A: 

My absolute favorite is cw.

Dario
+1  A: 

I had a few on my old blog:

I also have an argnull code snippet that inserts a Guard Clause that checks an argument for null and throws an ArgumentNullException, but I haven't gotten around to post that yet.

Mark Seemann
+1  A: 

VB has a lot that are pre-defined, but there are only a handful for C#. Do you have any really useful ones for C#?

Microsoft have released a whole bunch of C# snippets that bring it up to parity with the ones for Visual Basic. You can download them here:

http://msdn.microsoft.com/en-gb/vstudio/aa718338.aspx

Andy Patrick
cool, thanks. I didn't see these...
TheImirOfGroofunkistan
+1  A: 

Just to update an older thread... here's a link for Visual Studio 2008 C# code snippet download.

VS 2008 C# Code Snippet Download

Craig