Friday, June 27, 2008

Visual Studio 2008 and .NET Framework 3.5 SP1 Available Now

The public beta of Visual Studio 2008 and .NET Framework 3.5 SP1 are now available here.

SP1 comes with a slew of enhancements and fixes covering various aspects of the VS 2008 IDE as well as the .NET Framework.

Among the highlights relevant to ASP.NET and web development in the upcoming SP1 release include:

  • ASP.NET Data Scaffolding Support (ASP.NET Dynamic Data)
  • ASP.NET Routing Engine (System.Web.Routing)
  • ASP.NET AJAX Back/Forward Button History Support
  • ASP.NET AJAX Script Combining Support
  • VS 2008 Performance Improvements in HTML Designer and HTML Source Editor
  • VS 2008 JavaScript Script Formatting and Code Preferences
  • Better VS Javascript Intellisense for Multiple Javascript/AJAX Frameworks
  • VS Refactoring Support for WCF Services in ASP.NET Projects
  • VS Support for Classic ASP Intellisense and Debugging
  • Visual Web Developer Express Edition support for Class Library and Web Application Projects
  • ASP.NET Application Request Throughput Improvements of Up to 10%
  • SQL 2008 Support in VS 2008
  • ADO.NET Entity Framework and LINQ to Entities
  • ADO.NET Data Services
  • WCF Development Improvements
  • VB and C# Improvements

Other enhancements not mentioned above cover primarily client development (Windows Forms, WPF, and Setup Package). The SP1 releases are expected to be shipped this summer as free updates.

For a more detailed description of the new features in the release, check out Scott Guthrie's blog post or Somasegar's blog post.



Thursday, June 5, 2008

Passing a Variable Number of Arguments to a Method


If you need to pass an unknown number of arguments to a method, the params keyword is exactly what you need. With params, you can pass a variable number of parameters to a method and any parameters after the fixed parameters will be collected into an array and passed to your method. You can only use the params keyword for one parameter in your method declaration and it must always be the last parameter. A method that accepts a string and then a variable number of parameters of any type looks like this:
public void ObjectParams(string Message, params object[] p)
{
Console.WriteLine(Message);
for (int i = 0; i < p.Length; i++)
{
Console.WriteLine(p[i]);
}
}

This method prints the string parameter to the console then loops through variable parameters, printing each one to the console as well. It accepts an array of objects, so the parameters can be any data type. You would call the function like this:
ObjectParams("Variable Object Parameters", 12, 'z', "Test");

If you know the data type of your arguments, you can optimize this somewhat by specifying a type other than object for the variable parameters. A method that expects a variable number of integers would look like this:
public void IntParams(string Message, params int[] p)
{
Console.WriteLine(Message);
for (int i = 0; i < p.Length; i++)
{
Console.WriteLine(p[i]);
}
}

You call this method the same way as before, only passing integers.
IntParams("Variable Integer Parameters", 1, 2, 3, 4, 5);

You also can pass an array as the last parameter instead of passing individual parameters. The previous method, which accepts integers, also could be called this way.
int[] TestIntArray = new int[5] { 11, 12, 13, 14, 15 };
IntParams("Integer Array Parameter", TestIntArray);

The ability for a method to accept a variable number of parameters can come in handy in many situations. Now, with the params keyword, you have what you need to take advantage of this ability in C#.

String Literals Without Escape Characters

Today's tip is a very simple one, but if you are like me you will find yourself using it frequently. Do you have string literals that are filled with escape characters because they include characters that have a special meaning? One of the most common places you will find this is in file paths. If you don't want to have to worry about getting all of the backslashes correct, try using an @-quoted string instead. Say you have a variable like the following in your application:

string AppDataFile = "C:\\Program Files\\My Application\\MyApp.dat";

This works fine, but can be hard to read and is more likely to be prone to errors if you do not escape all of the backslashes correctly. Another option, using @-quoted strings, removes the need to escape the backslashes. The @-quoted version of the above example looks like this:

string AppDataFile = @"C:\Program Files\My Application\MyApp.dat";

The only changes are the addition of the @ sign before the opening quotation mark and the removal of the backslash to escape the other backslashes. This format matches the way you see file and directory paths displayed everywhere else in Windows. You can use @-quoted strings for more than paths, so take a look at your use of string literals and see where you can simplify you code and make it more readable.

Tuesday, June 3, 2008

Increase your productivity using visual studio 2008

Basic Building Commands:

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

Download the Visual C# 2008 Keybinding Reference Poster.

Use GhostDoc to create XML Comments

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:


This generates the following documentation:

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:

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:

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...

The property is created for you:

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:

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...


Visual Studio generates the following test method: