Home Page
"Join my free Amazing ASP.NET newsletter"
It's safe and easy to signup. You get:
  • Notified anytime a new article comes out
  • Technical step-by-step tutorials on all important ASP.NET topics
  • Expert guidance so you can reach unlimited ASP.NET success
  • Important ASP.NET news updates
Enter your Email

The .NET 4.0 Framework is right around the corner.  Yeah I know, a lot of you are probably still getting a handle on all the cool features in the 3.5 framework and Visual Studio 2008 but Visual Studio 2010 is coming soon.  In this article, I’m going to walk you through some of the major new features in CSharp C# 4.0 which is going to be part of .NET 4.0.  This article is based on a document by Microsoft’s Mads Torgersen called “New features in C# 4.0″.  I feel that some of these new features (such as named and optional parameters) will really help the great language of C# become even better.  In this article I will also suggest ways that I plan to use the new features of C# 4.0 to improve my applications so that you can begin thinking about the next iteration of design improvements for you own projects.

It appears that Microsoft is stressing flexibility in the new CSharp C# 4.0.  Microsoft has made an attempt to address some of the C# language’s constraints by offering additional ways of doing things for certain types of coding scenarios in C# 4.0.

Named and Optional parameters for Methods

In C# 3.5 and before you have to pass all of the parameters that a method is expecting in order to be able to compile the application.  Take a look at this method declaration:

public DataSet SearchDatabaseTable(string first_name, string last_name)

{

  //perform database search and return DataSet object

}

In order to call that method I would have to pass two string values:

//call the method by passing both expected parameters

DataSet mydata = SearchDatabaseTable("Martha", "Livingston");

This is all fine, but when you want to create methods that are more flexible and more generic you may have to create additional versions of the same method.  For example, let’s say I wanted to have the same method SearchDatabaseTable but I wanted the flexibility to be able to search by first name by itself, last name by itself or both first name and last name together.  With this scenario I would have to create three different versions of essentially the same method, but with different parameters declared (three different method signatures):

public DataSet SearchDatabaseTable(string first_name, string last_name)

public DataSet SearchDatabaseTableByFirstName(string first_name)

public DataSet SearchDatabaseTableByLastName(string last_name)

That seems like extra work just to get this requirement implemented doesn’t it?  Well, there is another option that I have been using for years that solves this problem or at least creates an acceptable workaround.  The way I have been handling this is by creating only one generic method like SearchDatabaseTable and then passing parameters with certain predefined values to indicate that a parameter should be ignored by the method.  For example, I may pass “” (the empty string) for a string argument to indicate that the Method should ignore that parameter.  Or I may pass -1 for an Integer parameter to indicate that the Method should ignore that parameter.

This workaround seems to be okay and it works fine, however it suffers from some shortcomings.  First the caller must still pass all of the method arguments, regardless of whether or not all of the parameters are being used.  Another problem is that the caller of the method must know what the values of the parameters are which indicate that a particular parameters should be ignored.  For example the caller of my method would need to know to pass -1 to have an Integer parameter ignored.  While these are certainly not showstopper problems it would be nice to have an easier and better way to handle the requirement.  In fact, when working with database searches, you may find yourself needing flexibility in order to support a variety of search options; I know I certainly would like some more flexibility without the extra coding.

C# 4.0 has added two new features to address this kind of problem.  Those features are: Named parameters for methods and Optional parameters for methods.

In order to make a method parameter Optional, in C# 4.0 you provide a default value for the parameter in the method declaration:

public DataSet SearchDatabaseTable(string first_name = "", string last_name = "")

Notice in this example that I provided default empty string “” values for both of the method parameters.  I can then call the method in any of the following ways:

//Passing both values

DataSet mydata = SearchDatabaseTable("Martha", "Livingston");

//OR Passing no values, the method will use the defaults

DataSet mydata = SearchDatabaseTable();

//OR Passing only the value for first name,

//the method will use the default value for last name

DataSet mydata = SearchDatabaseTable("Martha");

The other new option with methods is to call them using Named parameters.  To call a method using Named parameters you have to prefix a parameter value with the parameter name and a colon : character.  Take a look at the following alternative ways to call the SearchDatabaseTable method:

//Passing both values by name

DataSet mydata = SearchDatabaseTable(first_name: "Martha", last_name: "Livingston");

//OR Passing both values by name but in a different order

DataSet mydata = SearchDatabaseTable(last_name: "Livingston", first_name: "Martha");

//OR Passing only the last name parameter

//the method will use the default value for first name

DataSet mydata = SearchDatabaseTable(last_name: "Livingston");

//OR Passing only the first name parameter

//the method will use the default value for last name

DataSet mydata = SearchDatabaseTable(first_name: "Martha");

You can see that by using Named parameters, you have a lot more flexibility when calling a method.  I think that is great personally.

Stay tuned for the next article where I will cover more of the new features in C# 4.0.  Until then, what are you looking forward to in C# 4.0?