I want to iterate a list and get label text values. Everything works fine apparently, expect by Resharper tip. The code:
string someString = GetLocalResourceObject(GetNameAsResource(resourceKey)) != null ?
GetLocalResourceObject(GetNameAsResource(resourceKey)).ToString() :
parametro.Parametro.Nome;
And in GetLocalResourceObject(GetNameAsResource(resourceKey)).ToString() Resharper suggest: Possible ‘System.NullReferenceException’
I’m doing something wrong or Resharper just not recognize the previous null verification?
Tks
Since
GetLocalResourceObject()is a method, ReSharper doesn’t know that the return value from the second invocation is going to be the same as the return value from the first invocation! Do this:and the warning will go away, because now ReSharper knows that
resourceObjecthas been checked for nullity before having a method invoked on it.It might be better to look into the Null Object Pattern – that is, in the case where
GetLocalResourceObjectcurrently returnsnull, have it instead return an actual object that represents ‘no resource’. Then you wouldn’t need this nullity check at all.