10 Tips to Boost Your Productivity with C# and Visual Studio 2008


Learn Key Bindings


It's an obvious and trivial thing, but the timesaving will add up, especially for those actions you perform tens or hundreds of times a day such as building and debugging.  Here are some basic bindings every Visual Studio developer should know:



  • Build: CTRL + SHIFT + B
  • Word completion: CTRL + SPACE
  • Start with debugging: F5
  • Start without debugging: CTRL + F5

Even expert Visual Studio developers can benefit by learning new bindings. Download the Visual C# 2008 Keybinding Reference Poster and hang it in your work area.


 


Generate XML Comments with GhostDoc


Instead of typing XML comments by hand, let a tool do the work for you. Although macros and snippets are reasonably effective for this, I would recommend Ghost Doc over any other solution. This free add-in uses customizable templates to generate consistent, English-readable documentation based on the current context.  To use it, right-click (or use CTRL + SHIFT + D) to document the current element.  For example:


image


 


This generates the following documentation (note GhostDoc split the property name into words and created a sentence from it):


image


 


 


Auto-Implement Properties


Take advantage of a new feature of C#: auto-implemented properties. Rather than creating a private backing field for your properties, let the compiler do it for you. The following demonstrates the syntax:


image


Use the code snippet to make this even faster.  Type prop (the shortcut for an auto-implemented property) followed by TAB TAB.  Then fill in the data type and property name:


image 


 


Refactor


The refactor feature in Visual Studio is indispensable for many tasks, especially renaming, but one productivity feature I particularly like is Encapsulate Field. If you are unable to use an auto-implemented property, declare a private field and let Visual Studio generate the Property for you.  To use this feature, right-click on the field and select Refactor > Encapsulate Field...


 


 image


The property is created for you:


image


 


 


 


Add Commands to Visual Studio 2008


Install the PowerCommands for Visual Studio 2008 to add several productivity commands such as:



  • Close all documents
  • Copy and paste a class (automatically renames)
  • Remove and sort using statements project-wide
  • Copy and paste references (including a project reference)

Install the Team Foundation Server Power Tools to add several TFS productivity commands such as:



  • Find in source control

  • Open source folder in Windows Explorer

  • Work item templates (can be used to set values on multiple work items at once)

Add your own productivity commands.  For example, to add Reflector so it automatically opens on the current project.



  • Select Tools > External Tools

  • Click Add

  • Name it Reflector and browse to the executable

  • Enter $(TargetPath) for the Arguments

 


Speed up Compilation with Project Configuration


You may build tens of times during a programming session, so don't enable anything that isn't absolute necessary such as code analysis and XML documentation.  Develop in Debug configuration, and switch to Release configuration just before check-in to run code analysis and generate XML documentation. In a large solution I recently worked on, this shaved a minute off compilation time.


The following shows code analysis disabled in Debug configuration:


 


 


 image


The following shows code analysis enabled in Release configuration:


image 


 


Let Visual Studio Generate Unit Test Code


Although it can't fully automate unit testing yet (check out Pex), Visual Studio does a good job of generating positive unit test code to give you a jump start.  To use this feature, right-click on an element you would like to test and select Create Unit Tests... 


image


Visual Studio generates the following test method:


image


 


Use Interface-Driven-Design


You probably never thought of an interface as a productivity feature, but it can be if your development process is driven by contracts instead of implementation.  Let me illustrate with a simplified example.  One developer owns the business layer and another developer owns the data access layer, and they need to agree on how to communicate to implement a new feature.  In some business object designs, business components will instantiate data components (or call a static method).  This is a problem from a design standpoint because the two become tightly coupled.  It is also a problem from the productivity standpoint because the business layer developer becomes dependent on the data access layer developer's implementation.  Interface-driven design (IDD) solves this issue.


Rather than the business component developer waiting on the data component developer, they meet to design and implement the interface.  Both developers are then free to go their separate ways and implement the components in parallel.  IDD also enables the business developer to mock the data access component, thereby removing any scheduling dependencies.  The following illustrates the design:     


image


Make a Mockery of Dependencies


Different developers may own layers or features that your component is dependent on, but don't let that slow you down.  Suppose you are responsible for the business layer which depends on the data access layer which in turn depends on database tables and stored procedures. Rather than waiting for dependent layers to be completed, mock the data access layer so you can implement and unit test the business layer. By the way, you should be using mocks anyway; otherwise your unit tests are more than likely integration tests.  I recommend Rhino Mocks.


Here is a sample unit test with mocks:


image


Data Drive Unit Tests


If you have a unit test that multiple inputs to fully test, you could write a test method for every possible combination, but data-driven unit tests are more efficient.  When the unit test is run, it loads data from a table and calls the unit test for each row.  You can access the data in the current row using the TestContext.DataRow property.


image


Once the test completes, you can view the results:


image

Comments (31)

  1. Frank W Holt Jr says:

    Great blog! I especially like the ‘Data Driven Testing’ piece. I’m getting into this area and have been running a script for each test to prepare the data. I’ll give this a shot. Thanks.

  2. Hosam Kamel says:

    Some tips posted by John W Powell about how to increase your development productivity using Visual Studio

  3. Garry Trinder says:

    More tips:

    11. Use ReSharper (www.jetbrains.com)

    12. Use SonicFileFinder (sonicfilefinder.jens-schaller.de – from the co-worker of the GhostDoc author)

  4. John Powell says:

    Thanks for the feedback!

    Unfortunately ReSharper isn’t available for Visual Studio 2008 (yet)

  5. Morgan Cheng says:

    This post is really helpful!!!

  6. Karl Agius says:

    Excellent advice and references, thanks!!!

  7. Let Visual Studio Generate Unit Test Code — Makes my skin crawl.  

    Why, because if you allow test to be generated you may not fully understand the intent of the test.  Making it almost useless.

    Don’t get me wrong, having generated tests is better than NO tests at all, but this is the lazy mans version of testing if you ask me.

    http://devlicio.us/blogs/derik_whittaker/archive/2008/03/26/39701.aspx

  8. Here is the latest in my link-listing series .  Also check out my ASP.NET Tips, Tricks and Tutorials

  9. Here is the latest in my link-listing series .  Also check out my ASP.NET Tips, Tricks and Tutorials

  10. Here is the latest in my link-listing series .  Also check out my ASP.NET Tips, Tricks and Tutorials

  11. To generate XML Comments for fields, methods, properties, etc. you can tap forward slash 3 times while your cursor was just before the declaration line. Like this (| represents your cursor):

    |

    public bool IsCool { get; set; }

    ->

    ///|

    public bool IsCool { get; set; }

    ->

    /// <summary>

    /// |

    /// </summary>

    public bool IsCool { get; set; }

    This is built-in to Visual Studio.

  12. Ricardo says:

    The prop snippet in VS 2008 is not very efficient, at least not as good as the one in 2005, which would actually generate the get and set bodies.  I ended up copying the one from 2005 to 2008 and that’s the one I use!

  13. 【原文地址】 March 28th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, Silverlight, .NET 【原文发表日期

  14. Kyralessa says:

    Most of those tips look handy (though most I already knew), but that GhostDoc thing looks awful.  Auto-generated "documentation" that documents IsCool with "Gets or sets a value indicating whether this instance is cool" is worse than useless.  It’s worse because it’s a time-waster: You consult the documentation for help, and all it does is waste your time telling you something you already knew from the name.

    Better to have *no* documentation than to have reams of pointless "documentation" like that.

  15. dthomelin says:

    Boosting productivity is first a good help,

    I have visual studio 2008 with MSDN Help and I found that source selection that is available when installing MSDN help is irelevant.

    One need when developping web aspx for example to be able to restrict full text search within help to a few Sources like C# or Source System

    Currently search is giving result for a search througout tons of sources like sharepoint, windows fundation,

    widows forms or mobile meaning that help search is awfull

    is there a way to choses the specific sources that you want to search.

    Don say filter, it works only for keyword list.

  16. Kemaren setelah usai diajak mengabdi kepada negara oleh bang naren , maka gua melakukan blogwalking di

  17. tourist.Tam says:

    Plenty of whinny in the comment …. 🙁

    Anyway, thanks a lot for the few tricks that I had no knowledge of. 😉

    Specially the mockery (wording that I wouldn’t have though about to describe that).

    Tam

  18. Chris Staley says:

    ReSharper does support VS 2008.  3.1 (the current release version) will handle all of your C# 2.0 needs.  RS 4.0 is still in alpha/beta/whatever, but the nightly builds (http://www.jetbrains.net/confluence/display/ReSharper/ReSharper+4.0+Nightly+Builds) have proven useful in the interim for working with the new C# 3.0 language enhancements.

  19. JohnWPowell分享了在使用C#和VS2008时如何提高生产力的几个技巧和诀窍。

    http://blogs.msdn.com/johnwpowell/archive/2008/03/23

  20. Dflying Chen says:

    摘要本期共有9篇文章:提高C#和VisualStudio2008生产力的10个技巧ASP.NETMVCAction过滤器:缓存和压缩程序停止工作及其解决方法:第一部分:死锁调用D…

  21. LearnKeyBindings

    It’sanobviousandtrivialthing,butthetimesavingwilladdup,especiallyfo…

  22. dreamliner says:

    明显而又简单,但是节省时间,尤其是对于那些你每天都要执行上百次的操作,例如构建和调试。下面是一些每个Visual Studio都应该知道的一些基本的快捷键:

  23. Jacky_Xu says:

    摘要

    本期共有9篇文章: 提高C#和VisualStudio2008生产力的10个技巧

    ASP.NETMVCAction过滤器:缓存和压缩

    程序停止工作及其解决方法:第一部分…

  24. So Microsoft lets the community smoke clear from the last time a softie tried to sell the idea of reverse-engineering tests from code, and we’re right back to where we were a couple of years ago.  Zero progress.  Zero growth.  And Microsoft still trying to take advantage of its customers by working to convince them to use features that reduce customers’ success potential.

    Is it so hard to provide guidance based on practices that yield the greatest results rather than guidance that merely justifies the investment you’ve made in questionable features?

    The crisis of quality and consciousness continues in the Microsoft sphere of influence and coercion.

  25. Jack says:

    Good set of tips. I especially like Ghostdoc – it is a time saver for me because I like my code to be as fully documented as possible. Seeing methods without XML documentation can be really frustrating for me.

    Another tool you may or may not have heard of is Code Rocket. Its not free but saves a lot of time by allowing you to flesh out / visualise methods using pseudocode and flowcharts inside Visual Studio and then generating skeleton code. Not a free tool but you can download a free trial here: http://www.getcoderocket.com

  26. Jayesh says:

    This is very good C# tips.

    To learn more C# Tips with Examples visit this blog.

    jayeshsorathia.blogspot.com/…/c-tips.html

  27. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA says:

    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

  28. SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS says:

    SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS

Skip to main content