To get a trial key
fill out the form below
Team License (a basic version)
Enterprise License (extended version)
* By clicking this button you agree to our Privacy Policy statement

Request our prices
New License
License Renewal
--Select currency--
USD
EUR
GBP
RUB
* By clicking this button you agree to our Privacy Policy statement

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

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

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

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.

>
>
>
Examples of errors detected by the V310…

Examples of errors detected by the V3108 diagnostic

V3108. It is not recommended to return null or throw exceptions from 'ToString()' method.


Orchard CMS

V3108 It is not recommended to return 'null' from 'ToString()' method. SnippetHtmlExtensions.cs 60


public override string ToString() {
    ....
    return null;
}

FastReport

V3108 It is not recommended to return 'null' from 'ToString()' method. Variant.cs 1519


public override string ToString() {
  if (_value == null) return null;
  return this.String;
}

.NET Core Libraries (CoreFX)

V3108 It is not recommended to return 'null' from 'ToString()' method. StringAttributeCollection.cs 57


public override string ToString()
{
  if (Count <= 0) return null; // <=

  StringBuilder sb = new StringBuilder();
  foreach (string str in this)
  {
    ....
    sb.Append(str.Trim());
    sb.Append(',');
  }

  if (sb.Length > 0) sb.Length = sb.Length - 1;
  return sb.Length == 0 ? null : sb.ToString(); // <=
}

Similar errors can be found in some other places:

  • V3108 It is not recommended to return 'null' from 'ToString()' method. StringAttributeCollection.cs 71

.NET Core Libraries (CoreFX)

V3108 It is not recommended to return 'null' from 'ToString()' method. RepeatBehavior.cs 113


public struct RepeatBehavior : IFormattable
{
  ....
  public override string ToString()
  {
    return InternalToString(null, null); // <=
  }
  ....
}

internal string InternalToString(string format, IFormatProvider formatProvider)
{
  switch (_Type)
  {
    case RepeatBehaviorType.Forever:
      return "Forever";

    case RepeatBehaviorType.Count:
      StringBuilder sb = new StringBuilder();
      sb.AppendFormat(
        formatProvider,
        "{0:" + format + "}x",
        _Count);
      return sb.ToString();

    case RepeatBehaviorType.Duration:
      return _Duration.ToString();

    default:
      return null; // <=
    }
}

.NET Core Libraries (CoreFX)

V3108 It is not recommended to return 'null' from 'ToString()' method. JsonElement.cs 1460


public override string ToString()
{
  switch (TokenType)
  {
    ....
    case JsonTokenType.String:
      return GetString();
    ....
  }
}

public string GetString()
{
  ....
  return _parent.GetString(_idx, JsonTokenType.String);
}

internal string GetString(int index, JsonTokenType expectedType)
{
  ....

  if (tokenType == JsonTokenType.Null)
  {
    return null;
  }
  ....
}

ShareX

V3108 It is not recommended to return 'null' from 'ToString()' method. Logger.cs 167


public override string ToString()
{
  lock (loggerLock)
  {
    if (sbMessages != null && sbMessages.Length > 0)
    {
      return sbMessages.ToString();
    }

    return null;
   }
 }

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