Previous month:
January 2008
Next month:
March 2008

February 2008

Timesaver: Awesome New C# Property Syntax Shortcut

I just could not believe this! No, this cannot be, how did Microsoft know that I wanted this feature!

Well ... today I discovered this new shortcut writing properties in C# 3.0 using Visual Studio (actually I was using the C# Express edition). Lets say we want to have a class called person who has age and name. In C# 3.0 there is no explicit need to declare a variable and write get and set methods for the property.

I could just declare the class like this ...

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

And then use it in code like this

 Person p = new Person();
p.Name = "Me";
p.Age = 31;

This saves so much time. I just love this ...awesome way to define properties. Well I could also use the named parameters style code like this ...

Person p = new Person() { Name = "Me", Age = 31 };

C# has just gone crazy ...

How does this work?

Well if I try to decompile the code using Reflector then we would see that we have this following code for the class Person

internal class Person
{
    // Fields
    [CompilerGenerated]
    private int <Age>k__BackingField;
    [CompilerGenerated]
    private string <Name>k__BackingField;

    // Methods
    public Person();

    // Properties
    public int Age { [CompilerGenerated] get; [CompilerGenerated] set; }
    public string Name { [CompilerGenerated] get; [CompilerGenerated] set; }
}

Looks the like the following code was generated by the compiler ... I am still not clear how the "<PropertyName>k__BackingField" expression is used in sync with the compiler generated attribute, but I am happy that I have such a shortcut.

kick it on DotNetKicks.com

.NET Basics : Explicit vs Implicit Interface Implementation

Nearly all of us know that we can implement an interface in 2 ways, one is explicit and the other one is implicit. In case of the implicit implementation the interface methods stay public and in explicit implementation the methods stay private in C#. Let me show a simple example

interface ICar
{
   void Start();
}

Now this method can be implemented in two ways, the first one is an example of the implicit implementation

class BMW : ICar
{
    public void Start()
    {
        // write code to start the BMW
    }
}

See the method is public, it can also be protected as well. Now the explicit implementation should look like this

class BMW : ICar
{
    void ICar.Start()
    {
        // write code to start the BMW
    }
}

As you can see there is no public or private keyword present for explicit implementation, and if we try to put a access modifier like public or private then we would get a compiler error. However the C# compiler will make this method private.

Well, except for private and public is there any other significance?

No Virtual

Well yes, there is. An explicit method implementation cannot be virtual. So we cannot override the method implementation in a child class. If we had declared the method virtual in the implicit implementation like the code below then we would have been able to override in a subclass.

public virtual void Start();

So you can see the that if we had a specific implementation of BMW that needed a different way to start then we would be able to override the virtual function and call it.

No direct access for Derived class

Also the derived class cannot call the method directly, we cannot call base.Start() in a child class. We would need to cast it to the interface. So a derived class has to write code like this for explicit implementation.

ICar car = this;
car.Start();

No abstract Implementation

I almost forgot another issue, which is ... we cannot use explicit implementation in an abstract form. If we had defined the class BMW an abstract class we could have left the implementation abstract for a derived class to implement like this

abstract void ICar.Start();

But with explicit implementation we cannot delay the implementation in a later derived class.

Hmm ... when is explicit implementation a must?

Well, lets say we had another interface that has the same method with the same signature and then it could be a problem. Let see this example below

interface IBoat
{
    void Start();
}

Now lets assume we have a amphibian car than can run in both land and water but engine for water and land are different so it would require two different methods to start. Since the implicit implementation can have one method the same name in both cases if IBoat and ICar the same method would be called.

class MyAmphibianCar : ICar, IBoat
{
    public void Start()
    {
        // write code to start the BMW
    }
}

This is a problem since we want to have 2 different code to start the engine. So we would have to go to implicit implementation and write code like this

class BMW : ICar, IBoat
{
    void ICar.Start()
    {
        // write code to start the BMW
    }

    void IBoat.Start()
    {
        // write code to start the BMW
    }
}

Now our problem is solved and we can use two different codes for the two implementations.

When is Explicit Implementation a Problem?

If we use explicitly implement a value type then if we need to access the interface methods then we would have to cast the value type to the interface type which would cause boxing and may hinder performance.

kick it on DotNetKicks.com

Back in Dhaka, New Job

I could not make any post for about 2 weeks since I have been too busy with relocation from Kuala Lumpur to Dhaka. I have lived in Dhaka for over 30 years, but one year in KL has made me feel KL as homely as Dhaka, or even more. It is the city bursting with life at each and every moment.

I am no longer working for Pageflakes, a small internet based startup based in San Francisco, CA whose competitors are Netvibes, iGoogle, Live.com etc. I have re-joined Orion Informatics as Principal Software Architect which makes regulatory financial applications for US software firms whose end users are top banks in the US like Washington Mutual, Bank of America, HSBC and also regulatory bodies like US Federal Reserve, FFIEC etc. Since I have worked here for long, I feel like back at home.

One thing I miss most of KL, that is my 1.5 Megabit internet connection! Dear reader, please keep on reading ... I will be posting frequently now.