More on properties: parameterful properties

It’s been a long time since I’ve posted in this blog…and there are couple of good reasons for that. For starters, my PC is gone…the disk died and I won’t be replacing it soon. That means that now I’m using a 2 year old netbook for doing work at home and sometimes it gets pretty frustrating (especially when I need to use VS). Besides that, I’ve also been pretty busy working in my next books. Yes, that’s right: books. The HTML book I’ve mentioned before is almost ready and I’m also actively working in a JavaScript book which covers the ECMAScript5 specification (again, in Portuguese, for FCA). As you can see, I’ve been really busy in my free time . Anyways, I believe that things are more stable now and I think I’ll be able to be start writing more frequently in the next months. The idea is to keep writing about some C#/CLR basic concepts, a little but of JavaScript and (probably) pick up some new framework and start digging into it…

Before going on, I’d like to express my condolences to everyone who lost someone on the 20th February floods which hit us a year ago. Yes, it’s been a year, but I believe that this date will never be forgotten by any of us that were here at the time…

In the last technical post,I as talking about properties. And today,we’ll keep looking at them and we’ll talk about parameterful properties. Parameterful properties (a name which I’ve borrowed from Jeff Richter’s excellent CLR via C#) are properties whose get methods accept one or more parameters and whose set methods accept two or more parameters. In C#, these properties are known as indexers and are exposed as array-like properties. The next snippet shows how one can define a parameterful property and use it from C#:

//for demo purposes only…
public class IntArray {
    private int[] _arr;
    public IntArray(Int32 numberOfItems) {
        if( numberOfItems <= 0 ) {
            throw new ArgumentOutOfRangeException();
        }
        _arr = new Int32[numberOfItems];
    }

    public Int32 this[Int32 position] {
        get {
            if( position < 0 || position >= _arr.Length) {
                throw new ArgumentOutOfRangeException();
            }
            return _arr[position];
        }
        set {
            if (position < 0 || position >= _arr.Length) {
                throw new ArgumentOutOfRangeException();
            }
            _arr[position] = value;
        }
    }
}

var ints = new IntArray(10);
ints[0] = 10;
Console.WriteLine(ints[0]);

In the previous example(built only for demon purposes) ,the  paramertul property expects only one parameter in the getter and two in the setter (in the previous case, the first parameter passed to the getter and setter identifies the position and the second parameter ,passed only to the setter and introduced through the “hidden” value parameter, indicates the  value that is supposed to be put into that position).  As you can see, this is used as special name for a parameterful property. In practice, this means that you cannot use static parameterful properties in C# (even though the CLR does support that).The previous snippet shows some interesting recommendations too: for instance, notice that we’re throwing an exception when someone passes and indexer out of the expected interval.

Another interesting feature of parameterful properties is overriding: unlike parameterless parameters, you can override them by using different parameters. Form the CLR point of view, parameterful properties will also give place to setter and getter methods. In the previous example, that means that the code we’ve written will be transformed into something which looks like this:

public class IntArray {
    public Int32 get_Item(Int32 position){/*previous code here*/}
    public void set_Item(Int32 position, Int32 value){ /*previous code here*/}
}

As you can see, the compiler will automatically pick the name Item for a parameterful property and will prepend the getter and setter methods with the get_ and set_ prefix.

In C#, we never use the name Item when consuming a  parameterful property (the [] operator is used instead). However, if you’re writing a library that will be consumed from other CLR languages, then you can customize the name of this property by using the IndexerNameAttribute:

[IndexerName("Integer")]
public Int32 this[Int32 position] {/*previous code here*/}

By doing this, the compiler will automatically ge
nerate a pair of methods named get_Integer and set_Integer,
allowing other languages (ex.:VB.NET) to access this property through the Integer name. Notice that the String  type uses this attribute to let you access a char in a string from languages which don’t use the [] operator to interact with parameterful properties. Since you don’t use a name to refer to a indexer in C#, you’ll only be able to introduce one property of this “type” in your C# code (though, as I’ve mentioned previously, you can override it). This behavior might introduce some problems when you’re trying to consume  in C# a type written in another language which defines more than one parameterful property. For that type to be consumed from C#, it must indicate the name of the default parameterful property through the DefaultMemberAttribute (notice that  the C# compiler does this automatically for you C# types and it  does takes into  account the use of the IndexerNamAttribute). And yes, that will be the only parameter that C# code will be able to access…

btw, and before you ask, languages which don’t support parameterful properties can access them through direct getter and  setter method calls. And that’s it for now. Stay tuned form more.

Advertisements

~ by Luis Abreu on February 20, 2011.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: