You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Always check the result of an as operation (AV1570) If you use 'as' to obtain a certain interface reference from an object, always ensure that this operation does not return null. Failure to do so may cause a NullReferenceException at a much later stage if the object did not implement that interface.
New language features to consider:
is-patterns
Testing for interface implementation using as nowadays triggers a Resharper refactoring suggestion to replace the expression with an is-pattern. Example:
interfaceIUser{}classRemoteUser:IUser{}classExample{voidTest(IUseruser){varremoteUser=userasRemoteUser;// ~~~~~~~~~~~~~~ Resharper: Use pattern matchingif(remoteUser!=null){// ...}}}
which is then changed to:
if(userisRemoteUserremoteUser)
I think we should remove this rule, because it advocates outdated language usage.
Aside from that, the description is incorrect: "If you use 'as' to obtain a certain interface reference from an object..." should be reversed. An object either implements the interface, which makes the test redundant -or- the compiler issues an error because no conversion exists. The text should be: "If you use 'as' to safely upcast an interface reference to a certain object..."
The text was updated successfully, but these errors were encountered:
Existing rule:
Always check the result of an as operation (AV1570)
If you use 'as' to obtain a certain interface reference from an object, always ensure that this operation does not return null. Failure to do so may cause a NullReferenceException at a much later stage if the object did not implement that interface.
New language features to consider:
Testing for interface implementation using
as
nowadays triggers a Resharper refactoring suggestion to replace the expression with an is-pattern. Example:which is then changed to:
I think we should remove this rule, because it advocates outdated language usage.
Aside from that, the description is incorrect: "If you use 'as' to obtain a certain interface reference from an object..." should be reversed. An object either implements the interface, which makes the test redundant -or- the compiler issues an error because no conversion exists. The text should be: "If you use 'as' to safely upcast an interface reference to a certain object..."
The text was updated successfully, but these errors were encountered: