Saturday, July 16, 2011

"Default" property in C#?

I came across the "Default" keyword in VB.NET that seems to be handy in many cases. Following is the excerpt from msdn:

"A property that accepts arguments can be declared as the default property for a class. A default property is the property that Visual Basic will use when no specific property has been named for an object. Default properties are useful because they allow you to make your source code more compact by omitting frequently used property names."

I thought it would also be available in C# so that I can assign something like this: 

MyClass a = new MyClass();
a = "test";

I created a class and decorated with "DefaultProperty":

DefaultProperty("MyDefaultProperty")]
public class MyClass
{
    public string MyDefaultProperty;
}    

Now I created an instance of MyClass and assigned a string to the instance as below:

MyClass a = new MyClass();
a = "test";

But this gave me compilation error "Cannot implicitly convert type 'string' to MyClass'". Looks like there is no way to implement the "Default" behavior in C#!!

We could use operator overloading, which comes somewhat close, but it has its own disadvantage. Let us first use operator overloading and see what happens:

public class MyClass
{
           public string MyDefaultProperty;
           public string AnotherProperty;
           public static implicit operator MyClass(string value)
          {
             MyClass t = new MyClass();
             t.MyDefaultProperty = value;
           }
       }

The above  class has 2 properties, one of which is the default property. Now I don't get any compilation error when I use:

MyClass a = new MyClass();
a = "test";

This assigns the value "test" to "MyDefaultProperty" and so is equivalent to:

MyClass a = new MyClass();
a.MyDefaultProperty = “test”;

Have we achieved what we wanted? No!!  As I had mentioned earlier, this comes close to what we want and has its limitations. Now consider the following:

MyClass  a = new MyClass();
a.AnotherProperty = "another property";
a = "test";
What should be the value of a.AnotherProperty after line 3? It should be the string "another property". But as a new instance is created in the overloaded operator, the state of the object is gone and we get a brand new instance with only the default property set!!  

Is there any reason for not implementing this behavior in C# while it is available in VB.NET? It would be great if anyone throws some light on this.

Happy coding!!

4 comments:

Sudhir DBAKings said...

Nice post very helpful

dbakings

Bala said...

Thanks Sudhir!!

Jaiby said...

C# doesnt support default properties, alternative is use the implicit option, which you are already using.
cheers

Anonymous said...

Although this works you are creating two objects on the heap instead of just one. Depending on the size of the class and how many have to be created this may be an issue. The fix for this would to not declare your variable as a new instance of the class instead just declare it's type since the static method is going to create a new instance on the heap.

Post a Comment