Overload the array square bracket operator in C#
Although operator overloading is possible in C# (just like in C++), overloading the array square bracket operator is by definition not possible. However, C# provides an alternative called indexers. This is directly from the Microsoft C# Reference for the [] Operator:
The array indexing operator cannot be overloaded; however, types can define indexers, and properties that take one or more parameters. Indexer parameters are enclosed in square brackets, just like array indexes, but indexer parameters can be declared to be of any type, unlike array indexes, which must be integral.
The syntax for indexers resembles the C# syntax for properties rather than its syntax for operator overloading. Here’s a quick snippet for providing array-like access to a List you’d like to keep hidden within a class:
// overloads the square brackets to provide array-like access.
public T this[int i]
{
get { return this.List[i]; }
set { this.List[i] = value; }
}
Worked for me, and hopefully it works just as well for you!
UPDATE: As I do a quick search now, this Stack Overflow post just turned up.
To receive updates on new articles, subscribe to CRF Design today!
Similar Posts:
- Hibernate is missing in Windows XP
- Top 10 differences between Java and C#
- Using TZEdit on Windows SP, SP2, SP3 for Daylight Saving Time (DST)
- Why does an abstract class need to implement interface methods?
- MySQL increasing ID manually
Hibernate is missing in Windows XP
Not only is hibernate hidden from the shutdown/log-off options in Windows XP, it is also disabled by default. The procedure to enable hibernate is not difficult, but it took a while to figure out where the configuration settings were hidden. Hopefully posting it here will save someone some time.
Before you logout out of Windows, a window appears giving you an array of shutdown, standby, hibernate… options. Depending on your configuration, this may be a set of 3 buttons or a dropdown set of options. If you see a set of 3 buttons, holding shift at the window should display hibernate as an option. If the hibernate option does not display, go to the section below about Enabling Hibernate. If you see a window with a dropdown set of options and hibernate is not one of them, go to the section below about Enabling Hibernate.
Enabling Hibernate
- Go to the Control Panel > Power Options > Hibernate tab.
- Make sure Enable hibernation is checked.
- Click OK and hibernate should show up as one of your shutdown options.
Repetitively Enabling Hibernate
Maybe you have a lot of systems that require hibernation, or perhaps you’d like to include it in a set of scripts that you run when a system is built from scratch. You could run this command from the command prompt, or put it in a batch file:
powercfg /H:ON
Hope this helps!
To receive updates on new articles, subscribe to CRF Design today!











