Monday, June 11, 2007

Fórum með Hestana vestur

Við fórum með hestana vestur um helgina. Ég hef víst ekki nefnt hestana okkar áður en þeir eru Póló frá Miðjanesi og Nóra frá Njarðvík. Það verður að bíða betri tíma en einhvern tímann verð ég að setja niður á blað eitthvað um þessa frábæru hesta okkar.

Saturday, June 2, 2007

Extension Methods

C# 3.0 introduces a new feature called extension methods, which allow us to extend the functionality of an existing type without extending it's class. At times, extending a type's class can be difficult or even impossible, especially if a class is abstract, virtual or even sealed. Even in these cases extension methods will allow you to add a method to the type.
public static string Add(this string me, string stringToAdd)
return me+stringToAdd;
then a call...
Console.WriterLine("This is it".Add(", isn't it?"));
results in...
This is it, isn't it?
Caution required?

Because extension methods can be declared anywhere in the project, I can imagine maintaining another programmer's code, seeing a method that I find curious and wanting to know more about it. Now if the method is not declared in the type's class, then where is it? Who knows, could be anywhere in the project or even in any of the referenced assemblies. Could be difficult. But still a nice new feature.

Want more information?

Microsoft C# Future Versions

Implicitly typed local variables

C# 3.0 introduces a new construct called var that results in the compiler inferring the type of the variable from it's initializer, which is therefore obviously required.

Sample usage:
int a=3;
var b=7; // int is inferred from the '7' literal

string c="Stuff";
var d="Something"; // string is inferred from literal
C# is still strongly typed

Don't confuse implicit variable declarations with variables of scripting languages where a variable can hold different types of values throughout the execution of the program. A C# variable declared with the var construct is still a strongly typed variable that can only hold the same type of values as the initializer.

Complex and anonymous types

The var construct obviously simplifies instantiation of complex types (especially in the case of anonymous classes) and is therefore extremely important when it comes to LINQ (a feature to be discussed later).
// co1 and co2 of same type but co2 more readable?
Map<customer,arraylist<orders>> co1 = new
var co2 = Map<customer,arraylist<orders>>();

// anonymous class - discussed in later posting
var co3 = new {
However, I wonder if in some cases the var construct will make the program more difficult to read, in contrast to the sample above where the program is obviously easier to read as a result of using var. Can you think of such a situation?

Want more information?

Microsoft C# Future Versions

.NET Framework 3.0

As you may have noticed .NET Framework 3.0 has been installed on your machine!

What is this .NET Framework 3.0?

First of all, version 3.0 is not an upgrade from version 2.0 like version 2.0 was an upgrade from version 1.1, which was an upgrade from version 1.0. Actually, when version 3.0 is installed the installer starts by checking if .NET Framework 2.0 is installed, if not then version 2.0 gets downloaded and installed and then version 3.0 is installed. The reason is that the .NET Framework 3.0 is not really a framework!? :-)

What is it then?

In reality, .NET Framework 3.0 is a new set of APIs to be distributed with future versions of the .NET Framework. This collection used to be called WinFX but last year it was renamed to .NET Framework 3.0. That renaming didn't happen quietly as can be read at the WinFX MSDN blog site.

The new APIs coming with .NET Framework 3.0 are these:

  1. Windows Communcation Foundation (WCF)
  2. Windows Presentation Foundation (WPF)
  3. Windows Workflow Foundation (WF or WinWF)
  4. Windows CardSpace, a.k.a. InfoCard

These APIs are nice and it's good to have them installed but there are currently no applications that use them and I can't develop anything using them. Why install them?

How to develop for .NET Framework 3.0?

So, being a developer and all, I'm waiting for .NET Framework 3.5 (currently out in Beta 1 and expected to be released in the end of 2007). Why am I waiting for .NET Framework 3.5? It will contain the necessary tools to support the above new APIs. These tools are included in the up and coming Visual Studio Code Name "Orcas".

What about the new C# 3.0, is that in "Orcas"?

This new version of Visual Studio also comes with C# 3.0, or C# Orcas, which also comes with a set of new features that are just as interesting as the .NET Framework 3.0 APIs mentioned above. From the C# 3.0 Specification, these new language extensions include:

  1. Implicitly typed local variables, which permit the type of local variables to be inferred from the expressions used to initialize them.
  2. Extension methods, which make it possible to extend existing types and constructed types with additional methods.
  3. Lambda expressions, an evolution of anonymous methods that provides improved type inference and conversions to both delegate types and expression trees.
  4. Object initializers, which ease construction and initialization of objects.
  5. Anonymous types, which are tuple types automatically inferred and created from object initializers.
  6. Implicitly typed arrays, a form of array creation and initialization that infers the element type of the array from an array initializer.
  7. Query expressions, which provide a language integrated syntax for queries that is similar to relational and hierarchical query languages such as SQL and XQuery.
  8. Expression trees, which permit lambda expressions to be represented as data (expression trees) instead of as code (delegates).

Confused about all this?

Anyway, if you are still confused about .NET Framework 3.0 and all the new stuff coming from Microsoft right afer Vista why not read about Julia Lerman trying to explain it all!