“Indexed Properties” of a Class in C#

In Jabber-net library, which I am using for my client, I saw this piece of code, which basically stores an array of type-unsafe properties. It is a useful idiom to have in mind for C# developers.

/// <summary>

/// Sets or retrieves a connection property.

/// You have to know the type of the property based on the name.

/// For example, PORT is an integer.

/// </summary>

/// <param name=”prop”>The property to get or set.</param>

/// <returns></returns>

public object this[string prop]

{

get

{

if (!m_properties.Contains(prop))

return null;

return m_properties[prop];

}

set

{

m_properties[prop] = value;

if (PropertyChanged != null)

{

PropertyChanged(this, new PropertyChangedEventArgs(prop));

}

}

}

_________________________________

Used in this way (if stream is of this class):

stream[(string)parent.Tag] = GetControlValue(parent);

References:

While reading more about this, I learned it is called “Indexed Properties”.

http://msdn.microsoft.com/en-us/library/aa288464(VS.71).aspx

For preliminary on Indexers (like operator [] in C++):

http://msdn.microsoft.com/en-us/library/aa288465(VS.71).aspx

You must be logged in to post a comment.