티스토리 뷰

🌈 .NET MVC

The switch statement

James Wetzel 2022. 6. 16. 17:30
728x90
반응형
DisplayMeasurement(-4);  // Output: Measured value is -4; out of an acceptable range.
DisplayMeasurement(50);  // Output: Measured value is 50.
DisplayMeasurement(132);  // Output: Measured value is 132; out of an acceptable range.

void DisplayMeasurement(int measurement)
{
    switch (measurement)
    {
        case < 0:
        case > 100:
            Console.WriteLine($"Measured value is {measurement}; out of an acceptable range.");
            break;
        
        default:
            Console.WriteLine($"Measured value is {measurement}.");
            break;
    }
}

 

switch 와 enum 활용 예)

public enum WhiteIpRequestState
{
    NoRequest = 0,
    Requested = 1,
    Approved = 2,
    Rejected = 3
}

WhiteIpRequestState whiteIpRequestState = ...
           
switch(whiteIpRequestState)
{
    case WhiteIpRequestState.NoRequest:
    case WhiteIpRequestState.Rejected:
        return RedirectToAction("WhiteIpRequest", "Login");
    case WhiteIpRequestState.Requested:
        return RedirectToAction("WhiteIpRequestInProcess", "Login");
    default:
        break;
}

 

 

 

 

728x90
반응형