Thursday, November 20, 2008

C# 4 - Default Parameters

Coming from a C++ background, I'm really pleased that there'll eventually be default parameters built into the next version of C#. But I do think that C++ had a much cleaner way of handling them: it would only allow defaults from right to left, for instance:
void foo(int a, int b, int c=3, int d=4)
It wouldn't let you do:
void foo(int a=1, int b=2, int c, int d)
C# will, so for the example above, you'd end up with method calls that could look like:
foo( ,  , 3, 4)
One feature that I still really miss from C++ is const. This keyword could be applied to methods or parameters, so a method declared as:
void foo(int a, int b) const
{
}
would be guaranteed to not modify the state of the object (except for mutable members) A method declared as:
void foo (const Person person)
{
}
would be guaranteed to not modify the person. Maybe C# 5?

Wednesday, November 19, 2008

Adding folders to the Class View in Visual Studio

I didn't know this, but you can add folders to the Class View. You can then drag classes to it.   What's even better, you can drag methods to it!

Thanks to Sara Ford for her post describing this.

There's more good news! When you're editing code in the editor and you're over a class or method, you can use the 'Synchronise Class View' command (I've mapped mine to a keyboard short-cut, but you can add a toolbar item) to synchronise the Class View to the item you're currently editing.  Once synchronised, you can go to Class View and drag that into one of your folders.  Also, when you do this, you won't upset any of your team with changes to the solution or project files - because these are your settings, and hence are stored in the .suo file (which, on the other hand could be a pain, because user option files shouldn't be checked into your source repository).

Anyway, this is the next best thing to a 'bread-crumb' feature in the editor that let's you jump around to favourite places in the the code.   Maybe the bread-crumb feature will be in the next version of ReSharper?

Monday, October 20, 2008

Displaying values in the Visual Studio Debugger

Normally, when you display values in the Visual Studio debugger, you get formatting (quotes, tabs etc.) included. For instance, if you've got string s = "Hello[tab]World"; then hovering over s in the debugger displays "Hello\tWorld". If you wanted to copy a value value to the clipboard (maybe because you landed on an exception and you want to send the error to the idiot that caused it your fellow colleague), you'd probably right click on the value and select 'copy value'. This copies the value to the clipboard. Another way is to use the immediate window. Using the example above, typing 's' in the immediate window would produce:

 

To get rid of formatting (the quotes and the control characters), use the ,nq option:

 

This is useful in itself, but it really becomes invaluable if you want to show some XML. With the magical ,nq switch, the following code:

produces the following output in the immediate window:

Pretty useless, but if you add ,nq, you get:

Much nicer.

nq stands for nice'n quick, because it doesn't waste time writing quotes.

Probably.

Friday, June 27, 2008

Excluding Items From Code Coverage

There's an attribute in nCover that can be applied to your code to exclude it when doing code coverage.  This is handy for auto-generated types such as web services etc.

The attribute is in the nCover assembly, but most people wouldn't want to ship with their assemblies.  Fortunately, you can define your own attribute.  If you're using TestDriven.NET, call it 'CoverageExcludeAttribute', if not, call it what you want.

When using NCover, pass the name of the attribute in the /ea WhateverYouCalledTheAttribute command line attribute.

Here's the code, with comments.  Remember, don't put this in a namespace!

/// <summary> 
/// Use this attribute on types that do not need code coverage analysis. 
/// Such types are auto-generated types, such as web services etc. 
/// 
/// This attribute is used by TestDriven.NET to exclude the type/method in the 'test with coverage' functionality. 
/// This attribute can also be passed to NCover proper by using the /ea CoverageExcludeAttribute command line. 
/// </summary> 
/// <remarks> 
/// Note that this should NOT be part of a namespace! 
/// </remarks> 
[CoverageExclude] 
class CoverageExcludeAttribute : Attribute { }