C#C
C#3y ago
Hark Dand

✅ Indexer return collection or single object depending on parameter

Hi all,

is there any way to selectively return either a single object or a collection of multiple objects depending on the given parameter?
I want to achieve the following behavior:

Let's say I have some class which references a table by Rows (alphabetically, e.g. A, B, C, ...) and Columns (numerically, e.g. 1, 2, 3, ...)
If I want to have a reference to the cell A1 I would write the following:
public void SomeMethod() 
{
  var returnedObject = someClass["A1"]
}


Additionally, I would also like to be able to give ranges as the index. For example:
public void SomeMethod() {
  var returnedObjectCollection = someClass["A1:A11"]
  foreach (var o in returnedObjectCollection) 
  {
    // Do something here...
  }
}


Is there any way of achieving such a behaviour?

Thank you very much for your help!
Was this page helpful?