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

MyStack.push(40);
Console.WriteLine(MyStack+10);

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:

MyStack.push(50);
MyStack.push(60);
Console.WriteLine(10+MyStack+MyStack);

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

MyStack=10;

or

int value=MyStack;

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

int value = 1;
value += MyStack;
Console.WriteLine(value);

because the += is decomposed into

value=value+MyStack;

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:

MyStack.push(80);
int t = MyStack;
Console.WriteLine(t);

However you still can't write

MyStack=90;

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:

mike.james@i-programmer.info

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.

Banner


Linq and XML

XML, which is all about tree structured data, and Linq, which is all about querying collections, might not seem to fit together, but they work together just fine.



XML in C# - Using XElement

.NET has some really easy-to-use facilities for creating and editing XML. Many of these facilities were introduced to make Linq to XML work better, but you can make use of them in more general situati [ ... ]


Other Articles

<ASIN:1449380344>

<ASIN:0123745144>

<ASIN:0321658701>

<ASIN:0321741765>



Last Updated ( Wednesday, 13 April 2011 )
 
 

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