Archive for April, 2008

h1

Code Camp Oz Barbie

April 29, 2008

Here are some snaps from the barbecue that Readify hosted at Code camp Oz:

DSC00090DSC00091

DSC00097DSC00099 DSC00100 DSC00102 DSC00104 DSC00105 DSC00106 DSC00098

h1

Anonymous Types in C# 3.0

April 19, 2008

Anonymous Types allows developers to create a new type on the fly without an explicit declaration of the class. They can easily be explained with the help of an example -


var person0 = new
{
    FirstName = "Mahesh",
    LastName = "Krishnan",
    Height = 182
};
var person1 = new
{
    FirstName = "John",
    LastName = "Doe",
    Height = 165
};
var person2 = new
{
    LastName = "Doe",
    FirstName = "John",
    Height = 175
};

Notice that the syntax makes use of both Implicitly typed variables as well as Object initialization methods that were explained in earlier posts. The new keyword usually is followed by the type that we wish to create, but while creating anonymous types, this is left blank and is followed immediately with a curly bracket as shown above. When the C# compiler sees a new anonymous class declared, it creates a new class under the covers. If we look at the types of these anonymous classes, they will look something like this -

<>f__AnonymousType0`3[System.String,System.String,System.Int32]
<>f__AnonymousType0`3[System.String,System.String,System.Int32]
<>f__AnonymousType1`3[System.String,System.String,System.Int32]

 

Notice that C# automatically generated the same Type for both person0 and person1, as they contained the same elements in the same order. person2 had a different order of the same elements and so, C# created a new type for it.

Usage rules/restrictions

  • The properties in Anonymous types are all read only and therefore cannot be modified once they are created.
  • Anonymous types cannot have methods.
  • Anonymous types are always assigned to vars. This allows the compiler to assign the right type. But,  if Anonymous types are used as return values or as parameters in a function, they will have to be passed in as Objects, as var is not a proper type

Projection

Anonymous types also supports Projection. So, if we have a declaration as shown below -


var LastName = "Nurk";
var FirstName = "Fred";

var person4 = new { LastName, FirstName};

then a new Anonymous type will be created with the read only properties LastName and FirstName. C# automatically projects the names of the variables to the names of properties in the anonymous class.

This also works while using objects, as shown below -


Person personObject = new Person
{
    LastName = "Doe",
    FirstName = "Jane",
    Height = 156
};
var projectionFromClass = new
{
    personObject.FirstName,
    personObject.LastName
};

In this case, the object projectionFromClass will have an anonymous type that picks up the property names FirstName and LastName, which will hold the values “Jane” and “Doe”.

h1

Implicitly typed arrays in C# 3.0

April 17, 2008

After my C# 3.0 session at the Vic .NET Hands on day, there were several requests to post my examples in greater detail. So, I thought I’d continue the posts I started earlier on the same topic. The first 3 topic were -

Over a series of several posts, I plan to cover the remaining topics -

  • Implicitly Typed Arrays
  • Anonymous Types
  • Auto Implemented Properties
  • Extension Methods
  • Lambda Expression
  • LINQ
  • Expression Trees
  • Partial Methods

In this post, I plan to talk about Implicitly typed arrays. In C# 3.0, as with Implicitly typed local variables, you can also create an array of objects that use type inference to determine what array type they are. So the following statements would create an array of ints and doubles respectively, based on the types of the elements specified within the curly brackets -


//Examples of type inference
var myIntegers = new[] { 1, 2, 3, 4, 5 };
var myDoubles = new[] { 1, 2.1, 3.2, 4.3 }; 

Although the examples show primitive types as array elements, they are not limited to value types. The compiler will automatically infer the type based on the elements and assign it to the correct array type on the left hand side. The example below shows elements using a class Dog and its equivalent in C# 2.0


var myPets = new[]
    {
      new Dog( "Spike" ),
      new Dog( "Snoopy" )
    };

Dog[] pets = new Dog[2];
pets[0] = new Dog("Spike");
pets[0] = new Dog("Snoopy"); 

What you cannot do

For starters, you cannot mix and match types within your array. You also cannot have nulls when you have an array of value types. So, both the statements shown below are not legal -


//Cannot mix types
var mixedArray = new[] { 0, "one", 2, "three" };
//Cannot have nulls, when you mix with value types
var arrayWithNulls = new [] { 1, 2, null };

However, the following statement is acceptable, as it forces the compiler to create an array of nullable ints -


var arrayWithNulls = new[] { (int?)1, 2, 3, null };

Inference when using types from the same inheritance tree

When you have elements in the array that have types from the same inference tree, at least one of the types in the list have will have to be base class. For example, if we have the inheritance as shown below -

image

then, the first statement will not work, while the second one will -


var myPets = new[] { new Cat(), new Dog() } ;
var myPets1 = new[] { new Cat(), new Pet() } ;

If you want to create an array of IAnimal elements, then at least one of the elements in the array will have to be explicitly typed to an IAnimal as shown below -


IAnimal dog = new Dog();
var myPets2 = new[] { dog, new Cat() };

You cannot call it Implicitly typed, then. Can you? :)

h1

.NET 3.5 Enhancements Training Kit

April 15, 2008

Another link to go along with my earlier Download links post, is the one for the newly released .NET 3.5 Enhancement Training Kit - It can be downloaded from here.

It contains Hands on labs for all the new stuff such as ASP.NET MVC, ADO .NET Entity Framework and ADO.NET Data Services.

h1

Download Links

April 12, 2008

I promised the guys at the Victoria .NET Hands On day that I’ll post links to where Visual Studio 2008 Hands on labs can be found and where to get the VPC image with VS2008 et al installed. So, here they are:

Don’t forget that you need Virtual PC 2007 to use the VPC image

h1

Presentation on Silverlight 2.0

April 9, 2008

As part of RDN, I presented a ReadiDepth session on Silverlight 2.0 in Sydney yesterday. I will post the slide deck and samples on the Readify Web site soon, but if you missed the presentation and are in Melbourne tomorrow (Thursday 10th of October), please do drop by at Cliftons on Collins Street at 6:00pm.

For the early starters, there is also a morning session (7:00am) at Microsoft Theatre, Freshwater Place in Southbank.

If you are coming, don’t forget to register (AM Session, PM Session)