Archive for the ‘C# 3.0’ Category

h1

Extension Methods

May 30, 2008

In my series on the new C# features, I had previously blogged about -

In this post, I will be talking about Extension methods. Extension methods allow new methods to be added to an existing class without having to actually extend the class using inheritance.

For instance, if you wanted to add a function called ReverseString to the String class, you can add it without creating a new class that inherits from String. Of course, String being a sealed class cannot be extended and adding a function to it is not possible via inheritance.

The syntax for doing this looks like this -


public static string ReverseString(this string str)
{
   //code to reverse the string
}

Notice that the function has to be static and it takes this followed by the type to which the extension method needs to be added to as the first parameter.

Intellisense

Once an extension method has been created, it shows up in Intellisense - which is quite handy.

image

Notice that the icon for an extension method is different to the usual ones.

Usage Rules

When using extension methods, there are few rules that need to be followed -

  • Extension methods have to be declared in a static function in a top level static class
  • The first parameter has to start with “this” and it cannot appear multiple times in the parameters
  • The keyword “this” must be followed by the type that needs to be extended. The type can be an interface, abstract class, a concrete class or even a sealed class.
  • You cannot use things like “ref” and “out” with “this”
  • You cannot extend Properties, Events or operators (something for the future version, perhaps :) ?)

Ambiguity

If you have the same extension method declared in two separate static classes, then the compiler will throw an error message saying that the call is ambiguous when the actual function is called in code. Unlike namespaces that help resolve classes that share the same name, there is no way to resolve function name clashes.

Extension methods that currently exist

.NET framework 3.5 comes loaded with a lot of extension methods. For instance, the IEnumerable interface has extension methods such as Aggregate, Average, Concat, Count, GroupBy, etc. A lot of these extension methods are essential for implementing LINQ (which will be covered in a future post).

More Examples

Extension methods can have additional parameters, as this example shows -


public static string ExtensionMethodWithParameter(
                      this string str,
                      string value)
{
    //Do nothing, it is just an example
    return str;
}

You can also add extension methods to arrays -


public static float MyAverage(this int[] intArray)
{
    float sum = 0;
    foreach (var element in intArray)
    {
        sum += element;
    }
    return sum / intArray.Length;
}

They can also be added to the base class of them all - “object”. For example, this function converts any object into an Xml representation, based on its properties -


//Simple, shallow implementation. Converts any object to Xml
public static string ToXml(this object o)
{
    Type myType = o.GetType();

    XmlDocument xmlDoc = new XmlDocument();
    XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration(
        "1.0", "utf-8", null);

    // Create the root element
    XmlElement rootNode = xmlDoc.CreateElement(
        XmlConvert.EncodeName(myType.Name));
    xmlDoc.InsertBefore(xmlDeclaration,
        xmlDoc.DocumentElement);
    xmlDoc.AppendChild(rootNode);

    var properties = myType.GetProperties();
    foreach (PropertyInfo prop in properties)
    {

        XmlElement childNode =
            xmlDoc.CreateElement(prop.Name);
        childNode.AppendChild(
            xmlDoc.CreateTextNode(
            prop.GetValue(o, null).ToString()));

        rootNode.AppendChild(childNode);
    }

    return xmlDoc.InnerXml;

}
h1

C# Features talk at Ballarat

May 29, 2008

I’ll be giving a talk on C# 3.0 at the Ballarat .NET SIG. The talk will start at 6:00pm on the 11th of June at the Commander Center in Ballarat. After my presentation, Tatham Oddie will be giving his talk on ASP.NET MVC pattern. Come along, have a few pizzas/drinks, kick back and enjoy the presentations :)

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

Object Initialization in C# 3.0

February 19, 2008

In older versions of C# when you create a new object and had to initialize some of its members, you had to write code similar to this -

Name name = new Name();
name.FirstName = “Fred”;
name.LastName = “Nurk”;

To make this sort of initialization easier, C# 3.0 introduces new syntax as shown below -

//Initialization with empty constructor
Name name = new Name
             { FirstName = “Fred”,
               LastName = “Nurk” 
             };

The syntax also allows constructors with parameters -

//Intialization with constructor parameter
Name name = new Name(someParameter)
            {
                FirstName = “Fred”,
                LastName = “Nurk” 
            };

You can also do nested initialization as shown below -

//Nested initialization
Customer customer = new Customer
{
    CustomerName = new Name 
       { FirstName = “Fred” },
       Address = “400, Somwhere”
};

The syntax is pretty straight forward. You can initialize any public Property or member variable, by specifying the name of the Property/variable and initializing it with a = operator. More than one member can be initialized by using the comma operator. The only thing you cannot do is call a method or initialize a private member inside the initializer.

h1

Type inference/Implicitly Typed local variables

February 11, 2008

One of the new features in C# 3.0 is Implicitly Typed local variables. In scripting languages like JavaScript, you can define a variable like this -

var x = “some value”;

You can now do the same thing with C# 3.0 and although some people may consider this feature to be nothing more than syntactic sugar or a declaration shortcut  for lazy programmers, it is needed for declaring anonymous types and while using LINQ (which I’ll cover in future posts).

C# uses type inference to automatically detect the type specified on the right hand side of the declaration. So, all these declarations are valid:

var myName = “Mahesh Krishnan”;
var myHeightInCms = 182;
var myWeightInKgs = 76.432;
var myPet = new Cat();
var dict = new Dictionary<int, string>();

var b = (ans == “Y“) ? true : false; 

The main rule is that the variable has to be declared in one statement as shown above. Under the covers, C# substitutes  var with a string or an int or whatever type is specified on the right hand side of the declaration. This is done at compile time and as a result type safety is not compromised. When you use Visual Studio 2008, you will also notice that intellisense works correctly for the type used.

You cannot have an empty or null declaration

The C# compiler needs to know the type of variable it is creating. So an empty variable declaration or a variable declaration that is assigned to null is not allowed.

//Neither statements are valid
//
var uninitalized;

//var x = null;

Once a variable has been assigned, you cannot re-assign it to another type

In languages like JavaScript, you can assign an object to a variable and then re-assign another object of a different type to it. But in C#, this is not legal.

var myBooleanValue = true;           

//Cannot change types!
//myBooleanValue = “false”;

You cannot mix and match types

//Not allowed
var x = (myBooleanValue) ? true : “false”;

As I mentioned earlier, type inference is done during compile time and a statement as shown above is not supported as the type of x can only be determined at run time. As a result, this statement is not valid.

You cannot create an array of vars

Creating an array of var as shown below is not allowed:

//Not valid
//var[] arr = new int[] { 10, 20, 30 };

You can still assign an array to an array variable declared as a var as shown below -

 var arr = new int[] { 10, 20, 30 };

I will cover implicitly typed arrays in a later post.

Can only be used for local variables

One other major rule is that you cannot use var to declare member variables in a class. They are only allowed within a function body.

Use them wisely

Although, using var to declare a variable is a nice feature, use it wisely. Using it to declare known types such as integers and strings can make the code a bit unreadable. Another thing to avoid is using the keyword var as a variable. This is still allowed for backward compatibility, but you should avoid it at all costs.

(This is my second post on the new C# features. My first post on Property short cuts can be found here)

h1

Property shortcuts in C# 3.0

February 9, 2008

I gave a talk about the new features in C# 3.0 at the Victoria .NET Dev SIG back in December. It was a packed house, with over 100 people and the turn out surprised me considering the fact that it was less than 10 days to Christmas.

Anyway, I had promised I would post my examples to the attendees and one of them asked about it recently. So, I decided to get my act together and blog about it.

Changes to C# 3.0 include features that range from what people refer to as syntactic sugar to some really cool stuff that you can do with LINQ. And covering them all in one post will get too big (for me). So, I’ll do one feature at a time, starting with Property Shortcuts.

Property shortcuts

Typically in C# 2.0, you would implement a property as shown below -

private string _firstName;
public string FirstName
{
    get { return _firstName; }
    set { _firstName = value;}
}

It is a good thing to encapsulate your private data members, but more times than not, you will find yourself creating a wrapper for your private data members and nothing more. So, why not just expose these data members as public? Because, that would be bad practice - we would then lose the flexibility to change the implementation later.

C# 3.0 provides us with a compromise. You can now declare your property as shown below -

public string FirstName { get; set; }

By declaring it this way, there is no need to explicitly create a private data member to hold FirstName. So, you may ask - what is the difference between this and declaring the data member FirstName as public? The difference is that under the covers, C# creates a private data member for you. And if you decide to change the implementation later,  you can.

If you think that the declaration of FirstName is very similar to how you would declare it in an interface, you would be right. Declaration of FirstName in an interface will still look like this -

interface IName
{
    string FirstName { get; set; }
}

And if you wanted to declare this in an abstract class, then you can still do it -

abstract class AbstractName : IName
{
    public abstract string FirstName
        { get; set; }
}

Using the abstract keyword prevents the compiler from generating private data member for the property.

Is the old syntax supported? Absolutely. The only thing that will not work is a syntax that goes something like this -

// Will not work!
public string MyProperty { get; }

The reason for that is quite obvious, actually. If this syntax was indeed legal, how would you assign a value to the property? You can’t and because there is no explicit private data member, you cannot assign a value to that either. For the same reason, using a set; in isolation will also not work.