다음의 코드가 있다고 한다면,
public static IDrawingSession Create(object arg)
{
var canvasDrawingSession = arg as CanvasDrawingSession;
if (canvasDrawingSession is null)
throw new ArgumentException("The argument must be a CanvasDrawingSession instance.");
return new Win2dDrawSession(canvasDrawingSession);
}
패턴일치를 이용해서 다음처럼 개선할 수 있습니다.
public static IDrawingSession Create(object arg)
{
if (arg is not CanvasDrawingSession canvasDrawingSession)
throw new ArgumentException("The argument must be a CanvasDrawingSession instance.");
return new Win2dDrawSession(canvasDrawingSession);
}