C# Corner Listing #1: Avoid Redundant Indexers (C#)
public double this[string row, string column]
{
get
{
int rowIndex = 0;
while (rowIndex < rowlabels.count)="" {="" if="" (rowlabels[rowindex]="=" row)break;="" rowindex++;="" }="" if="" (rowindex="=" rowlabels.count)="" throw="" new="" argumentexception(="" "row="" label="" not="" found",="" "row");="" int="" colindex="0;" while="" (colindex="">< columnlabels.count)="" {="" if="" (columnlabels[coli]="=" column)="" break;="" colindex++;="" }="" if="" (colindex="=" columnlabels.count)="" throw="" new="" argumentexception(="" "column="" label="" not="" found",="" "column");="" return="" values[rowindex][colindex];="" ;="" }="" }="" public="" double="" this[int="" row,="" int="" column]="" {="" get="" {="" return="" values[row][column];="" }="">
Listing 1: These two methods perform the same task. The difference is the parameters you need to use. Internally, the version using integers will be quite a bit faster. However, there’s no way for your users to know that, without testing or diving into your code with Reflector.