In search of default properties
In search of default properties
Written by Mike James   
Wednesday, 13 April 2011
Article Index
In search of default properties
Operator overloading


Operator overloading

One way of creating a default property would be to overload the assignment operator for the type in question. This is a good idea but unlike C++ you can't overload the assignment operator in C#. You can override the + operator however and this can be used in combination to give += say. However as we will discover this isn't particularly useful.

For example to override the + operator you might use something like:

static public int operator +(int d, stack s)
return s.pop() + d;

Following this you can write


and the result will be 40+10. However if you want to do 10+MyStack you also need:

static public int operator +(int d, stack s)
return s.pop() + d;

These two custom operators allow you to use a stack in an arithmetic expression like:


You can also define custom operators for the other basic operators -, * and / just as easily and then you really can write arbitrary expressions involving a stack.

The one thing you cannot do is write



int value=MyStack;

because the assignment operator hasn't been overloaded. However you can write:

int value = 1;
value += MyStack;

because the += is decomposed into


and the + sign is overloaded correctly.

Implicit conversion

There are various ricks you can use to implement a default property that doesn't rely on the use of indexers or operator overloading but they only work in limited ways. You can however make the assignment

int value=MyStack;

work very easily.

All you have to do is define an implicit conversion operator which converts the MyStack object into a suitable int by accessing the "default" property. For example:

public static implicit operator int(stack ms)
return ms.pop();

Now you can write:

int t = MyStack;

However you still can't write


and there is no way that you can easily write an implicit conversion operator that converts an int to a stack. The reason is that you have no knowledge of the left hand side of the assignment in an implicit conversion operator - you only know about the right hand side i.e. the int in this case. Your only real choice is to generate a new stack object, store the int in it using push and return that to be assigned to MyStack. Of course this has the effect of resetting the state of the stack which is not what was required.

Is there a clever way of achieving the result using say introspection or the dynamic language run time?
I can't say but I'm still working on the problem.

If you have any solutions email me:

Until then these solutions are as close as we can get to default properties in C#.[


You can download the code from the CodeBin (note you have to register first).

If you would like to be informed about new articles on I Programmer you can either follow us on Twitter or Facebook or you can subscribe to our weekly newsletter.


Deep C# - Anonymous Methods, Lambdas And Closures

Anonymous methods aren't particularly new, but they have hidden depths and lead on to lambdas and the idea of a closure. These are all important ideas in modern programming.

Multicast Delegates and Events

Multicast delegates are useful in their own right but they also form the basis on which the C# event system is built. We take a close look at how they work and how to use them. For example, did you kn [ ... ]

Other Articles





Last Updated ( Wednesday, 13 April 2011 )

RSS feed of all content
I Programmer - full contents
Copyright © 2018 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.