V3105. The 'a' variable was used after it was assigned through null-conditional operator. NullReferenceException is possible.

This diagnostic warns you about the risk of getting 'NullReferenceException' and is triggered when a variable’s field is accessed without first testing that variable for null. The point here is that the value the variable refers to is computed using the null-conditional operator.

Consider the following example:

public int Foo (Person person)
{
  string parentName = person?.Parent.ToString();
  return parentName.Length;
}

When initializing the 'parentName' object, we assume that 'person' may be null. In that case, the 'ToString()' function will not be executed, and the 'parentName' variable will be assigned a null value. An attempt to read the 'Length' property from this variable will result in throwing 'NullReferenceException'.

This is what a correct version of the code above may look like:

public int Foo (Person person)
{
  string parentName = person?.Parent.ToString();
  return parentName?.Length ?? 0;
}

Now the function will return the string length if the 'parentName' variable does not refer to null, and 0 if it does.

According to Common Weakness Enumeration, potential errors found by using this diagnostic are classified as CWE-690.

You can look at examples of errors detected by the V3105 diagnostic.


Bugs Found

Checked Projects
386
Collected Errors
13 991
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site. Learn More →
Accept