ReSharper: how to remove “Possible 'System.NullReferenceException'” warning

后端 未结 6 1946
长情又很酷
长情又很酷 2020-12-11 02:41

Here is a piece of code:

IUser user = managerUser.GetUserById(UserId);
if ( user==null ) 
    throw new Exception(...);

Quote quote = new Quote(user.FullNam         


        
6条回答
  •  旧巷少年郎
    2020-12-11 03:22

    This is caused by the Resharper engine. These "possible NullReferenceException" happen because someone (probably at Resharper) has declared/configured somewhere an annotation on the method.

    Here is how it works: ReSharper NullReferenceException Analysis and Its Contracts

    Unfortunately, sometimes, these useful annotation are just wrong.

    When you detect an error, you should report it to JetBrains and they will update the annotations on the next release. They're used to this.

    Meanwhile, you can try to fix it by yourself. Read the article for more :)

提交回复
热议问题