Виртуальная песочница (тм)

Tuesday, July 6, 2010

.NET vs Delphi: the "as" operator


public object Execute(object request) {
if (request is Request)
return service.Execute(request as Request);
else
throw new NotImplementedException();
}


CA1800 : Microsoft.Performance : 'request', a parameter, is cast to type 'Request' multiple times in method 'Wrapper.Execute(object)'. Cache the result of the 'as' operator or direct cast in order to eliminate the redundant isint instruction.<br />

Microsoft хочет, чтобы было так:


public object Execute(object request)
{
var r = request as Request;
if (r!=null)
return service.Execute(r);
else
throw new NotImplementedException();
}


потому что, как оказывается, "the "as" operator never throws an exception. Instead, if the indicated conversion is not possible, the resulting value is null."

No comments: