sterbehilfe
sterbehilfe
CC#
Created by Hark Dand on 2/13/2023 in #help
✅ Indexer return collection or single object depending on parameter
Yeah, you can't return different types for same parameter types. What could work is returning a base class or an interface for both, but I don't think that would work well.
44 replies
CC#
Created by Hark Dand on 2/13/2023 in #help
✅ Indexer return collection or single object depending on parameter
Yes, you can create your own indexer in your class like this and create a method like "GetCell" for the logic:
public ReturnedObjectType this[string cell] => GetCell(cell);
public ReturnedObjectType this[string cell] => GetCell(cell);
But for your indexer returning a collection you will need a different signature, because you can't have two indexer with the same parameter types, for example:
public ReturnedObjectCollectionType this[string startCell, string endCell] => GetCellRange(startCell, endCell);
public ReturnedObjectCollectionType this[string startCell, string endCell] => GetCellRange(startCell, endCell);
44 replies