# Thursday, 13 May 2010

The var keyword was introduced in C# 3.0 and is used to declare a new variable.

The var keyword looks suspiciously like the variant keyword from Visual Basic 6. However, unlike variant, var is used to specify a strongly-typed variable. The type of the variable is inferred by the value initially assigned to that variable. As a result, whenever we use var to declare a variable, we must assign a value to the variable on the same line. So

var myString = "Hello";

is valid, but

var myString;

is not.

As mentioned earlier, the type of a variable is inferred from the value assigned to it, so in the line

var myString = "Hello";

is equivalent to

string myString = "Hello";

and

var myNum = 99;

is equivalent to

int myNum = 99;

The compiler is smart enough to recognize that "Hello" is a string and 99 is an integer and type the variable appropriately. This type checking is done at compile time, so the following code is invalid and will not compile

var x = 99;
x = "Hello";

because x is an integer, it is not possible to assign a string to it and the compiler knows this.

The var keyword is also smart enough to infer data types from the return types of functions. For example, if I have a function that returns an int and I assign the results of that function to a variable declared with the var keyword, that variable will be defined as an int. Here is a sample

static int AddNumbers(int x, int y)
{
return x + y;
}
var sum = AddNumbers(2,3);

The variable sum is an int because AddNumbers returns an int.

Earlier this year, following a user group presentation and someone noticed my use of the var keyword in my demos. The appropriateness of var became a Twitter debate and the subject of at least one blog post.

As long as I follow the rules, the compiler doesn't care whether I declare a variable using var or the explicit data type. So when do I use var? I use var when I think it will improve readability.

Consider the following 2 (equivalent) lines of code

Line 1: Customer cust = new Customer();
Line 2: var cust = new Customer();

Both lines are the same as far as the compiler is concerned. But the second line of code is more concise and easy to read. I have communicated the same information in a shorter command and with no loss of information. It should be obvious to anyone reading the second line that the variable cust contains a Customer object. In this case, I would declare the variable with the 'var' keyword because it is more readable.

The last example may not seem like there is much difference in readability between the two lines, but check out the following two equivalent lines

Line1: Dictionary<int, string> dict = new Dictionary<int, string>();
Line2: var dict = new Dictionary<int, string>();

Clearly, Line2 is more readable than Line1 in this case.

Now consider the following (equivalent) lines of code

Line 1: Customer cust = RetrievePrimaryCustomer();
Line 2: var cust = RetrievePrimaryCustomer();

Both lines are the same as far as the compiler is concerned. But I can't be sure, just from looking at the declaration, what data type is returned by the RetrievePrimaryCustomer method. The compiler knows the type, but I need to go through one or more manual steps to figure it out. In this case, the first option is more readable because it provides to anyone reading my code more information about the variable.

So my rule for using var is simple:

If the data type is obvious to anyone reading my code, use the var keyword; Otherwise explicitly specify the data type.

The var keyword is a useful shortcut that can help make your code more readable.

C#
Thursday, 13 May 2010 12:42:27 (GMT Daylight Time, UTC+01:00)
# Wednesday, 12 May 2010

Episode 87

In this interview, Day of .Net organizers John Hopkins and Jason Follas describe what went into planning this event and what were the results.

Wednesday, 12 May 2010 05:04:36 (GMT Daylight Time, UTC+01:00)
# Monday, 10 May 2010

Episode 86

In this episode, David Hoerster describes how he uses jQuery and WCF to create rich, interactive applications.

Monday, 10 May 2010 11:18:51 (GMT Daylight Time, UTC+01:00)
# Wednesday, 05 May 2010

Episode 85

In this show, David Isbitski and Matt Van Horn describe Behaviors, a feature that allows you to easily enhance the user interface of your Silverlight applications.

Wednesday, 05 May 2010 12:09:02 (GMT Daylight Time, UTC+01:00)

Microsoft Product Unit Manager Cameron Skinner came to the midwest to show off the Architecture features of Visual Studio 2010. He began his tour in the Detroit area, speaking at local companies in the afternoon and at the Great Lakes Area .Net User Group (GANG) in the evening. I recorded two of his presentations, which are available here.

Here is the presentation at GANG

Part 1:

Part 2:

Part 3:

Part 4:

Part 5:

Here is the presentation at a Detroit-area company.

Part 1:

Part 2:

Part 3:

Part 4:

Wednesday, 05 May 2010 03:04:35 (GMT Daylight Time, UTC+01:00)
# Monday, 03 May 2010

Episode 84

In this interview, John Petersen describes how to use jQuery, JSON and Ajax to enhance an ASP.Net MVC application.

Monday, 03 May 2010 11:28:46 (GMT Daylight Time, UTC+01:00)
# Friday, 30 April 2010

I began reading Agile Principles, Patterns and Practices in C# by Robert C Martin and Micah Martin after a friend recommended the chapters on pair programming.  My friend was right, of course. The Martins not only decribed pair programming but included an entertaining script of two developers pairing on a programming problem.

But, as I dove deeper into this book, I found a wealth of other information.

The book begins with a section on agile development, defining some basic terms and concepts recommended practices. It follows with a detailed section on good design practice. This second section is the most interesting, as it describes the famous SOLID principles. SOLID is an acronym for a set of good design practices:

S=Single Responsibility Principle: Each class should serve only one purpose and have only one reason to change.
O=Open-Close Principle: Classes should be open for extension but closed for modification
L=Liskov Substitution Principle: It should always be possible to substitute a derived class with its base class
I=Interface Segregation Principle: Interfaces implemented by a class are defined by the client objects that use that class; a class should implement a separate interface for each client that calls it.
D=Dependency Inversion Principle: To maintain flexibility, you should write code that depends on abstractions, such as interfaces.

Next, the authors present an overview of Unified Markup Language (UML), a graphical language used to describe software designs and requirements. Common UML diagrams and shapes are described and the author offers opinions of which ones are most useful and when to best use them.

The last half of the book is a case study of a Payroll System in which the authors use examples to illustrate the concepts introduced in the first half of the book.

Although C# is included in the title, the book does not focus on C# and almost none of the concepts are specific to any particular language. All the code examples are in C#, which makes it a bit more accessible if that is your strongest language.

The book is filled with lots of information and good advice. For example, the authors recommend an iterative approach to writing software, a test-first approach to development and encourage developers to refactoring their code frequently.

Whether you read all of Agile Principles, Patterns and Practices in C# or pick through the sections of interest, you will benefit from this book.

Friday, 30 April 2010 19:41:42 (GMT Daylight Time, UTC+01:00)
# Monday, 26 April 2010

Episode 83

In this interview, Eric Greene describes the advantages of using a Content Management System to rapidly build a flexible web site

Monday, 26 April 2010 16:02:50 (GMT Daylight Time, UTC+01:00)
# Monday, 19 April 2010

Episode 82

Monday, 19 April 2010 13:28:26 (GMT Daylight Time, UTC+01:00)
# Wednesday, 14 April 2010
  • Saturday April 17, I will be presenting "Building Your First ASP.Net MVC Application" at the Pittsburgh Code Camp at The University of Pittsburgh. More information is available at http://codecamppgh.com/codecamp.aspx

  • Saturday May 1, I will be presenting "Extending your Application with the Managed Extensibility Framework" at the Ann Arbor Day of .Net at Washtenaw Community College. More information is available at http://www.dayofdotnet.org/AnnArbor/Spring2010.

  • Friday August 7, I will be presenting "Effective Communication" at DevLink in Nashville, TN. More information is available at http://devlink.net.

Wednesday, 14 April 2010 11:57:00 (GMT Daylight Time, UTC+01:00)