Search code examples
c#filterenumsswitch-statementmultiple-return-values

Switch with enum and multiple return


I have two objects:

public enum BugReportStatus
{
    OpenUnassigned = 0,
    OpenAssigned = 1,
    ClosedAsResolved = 2,
    ClosedAsRejected = 3
}

and

public enum BugReportFilter
{
    Open = 1,
    ...
    Closed = 4,
}

And I would like to create a switch case where depending on the BugReportFilter choose my output will be a specific BugReportStaus.

So I created a method CheckFilter

private BugReportStatus Checkfilter(BugReportFilter filter)
{
    switch (filter)
    {
        case BugReportFilter.Open:
            return BugReportStatus.OpenAssigned;

        case BugReportFilter.Closed:
            return BugReportStatus.ClosedAsResolved;
    }
};

The problem is that in the case of a BugReportFilter.Open option I should return BugReportStatus.OpenAssigned AND BugReportStatus.OpenUnassigned, is there a way to concat this two options in a single return ?


Solution

  • You could return IEnumerable<BugReportStatus>:

    private IEnumerable<BugReportStatus> Checkfilter(BugReportFilter filter)
    {
        switch (filter)
        {
            case BugReportFilter.Open:
                return new[]{ BugReportStatus.OpenAssigned, BugReportStatus.OpenUnassigned };
    
            case BugReportFilter.Closed:
                return new[]{ BugReportStatus.ClosedAsResolved };
        }
    };
    

    Then you could use Enumerable.Contains to check if it's f.e. BugReportStatus.OpenAssigned:

    bool isOpenAssigned = Checkfilter(someFiler).Contains(BugReportStatus.OpenAssigned);