Our website uses cookies to enhance your browsing experience.
Accept
to the top
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS-Studio license for Microsoft MVP specialists
** By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
** By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
** By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
>
V3105. The 'a' variable was used after …
menu mobile close menu
Analyzer diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Diagnosis of micro-optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
Customer specific requests (C++)
MISRA errors
AUTOSAR errors
OWASP errors (C#)
Problems related to code analyzer
Additional information
toggle menu Contents

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

Apr 26 2021

This diagnostic warns you that a 'NullReferenceException' exception may be thrown during the program's execution. The analyzer issues a warning if you do not check a variable for 'null' and then call the variable's method or access its field or property. The key point is, the variable's value is calculated with an expression that uses the null-conditional operator.

Take a look at this example:

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

In the code above, when initializing the 'parentName' object, we assume 'person' can be 'null'. In this case the 'ToString()' function won't be executed, and 'null' will be written to the 'parentName' variable. When the execution flow accesses the 'parentName.Length' property, a 'NullReferenceException' is thrown.

You can fix the code as follows:

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

Now, if the 'parentName' variable is not 'null', the 'Foo' function returns the string length. Otherwise, it returns 0.

Here's one more example:

void DoSomething(string[] args)
{
  var nonNullStrings = args?.Where(arg => arg != null);
  foreach (var str in nonNullStrings)
    ....
}

Similar to the previous example, the 'nonNullStrings' variable initialization assumes the 'args' parameter can be 'null'. In that case, the 'nonNullStrings' variable is also assigned 'null'. When the 'foreach' loop iterates through the collection items, the 'GetEnumerator()' method is implicitly called for 'nonNullStrings'. If 'nonNullStrings' is 'null', a 'NullReferenceException' exception is thrown.

You can correct the code as follows:

void DoSomething(string[] args)
{
  var nonNullStrings =    args?.Where(arg => arg != null) 
                       ?? Enumerable.Empty<string>();

  foreach (var str in nonNullStrings)
    ....
}

This diagnostic is classified as:

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