简体   繁体   中英

Code Analysis c# .NET CA1822

I run Code Analysis and got this message:

CA1822 : Microsoft.Performance : The 'this' parameter (or 'Me' in Visual Basic) of 'CreateIntervalString(TimeSpan)' is never used. Mark the member as static (or Shared in Visual Basic) or use 'this'/'Me' in the method body or at least one property accessor, if appropriate.

My code is:

private string CreateIntervalString(TimeSpan timeSpan)
{
    return timeSpan.ToString();
}

as I understood, because CreateIntervalString function does not use any the members of the class, and only uses on the timeSpan input, VisualStudio recommends me to mark it as static.

My Questions:

  1. Why when I mark it as static, the performance is improved?
  2. My function is part of library that should be thread-safe, does marking method as static prevent this?
  3. I have additional private functions that use only its inputs, and does not use at any other members of the class, yet I don't get the same error for them.

Thanks a lot!

Examples:

the following method provides an error:

    private string CreateIntervalString(TimeSpan timeSpan)
    { 
          return timeSpan.ToString();
    }

and the following does not:

    private DateTime ParseDateString(string dateTimeString)
    {
     // the years,monthe,days,hours,minutes and secondes found by the dateTimeString input        
      return new DateTime(years, months, days, hours, minutes, secondes);
    }

The MSDN site http://msdn.microsoft.com/en-us/library/ms245046.aspx gives the answer to the performance aspect

If the method is not marked as static then the current object (this) will be checked against null by the runtime. In most cases there will be little observable difference, it's true, but if a method which is called millions of times per second can get that gain by being made static then it could be worthwhile.

  1. The performance is not improved (in any way that matters), but the code gets clearer. The method doesn't make the impression that it uses the instance, and you can use the method without creating an instance of the class.

  2. As you are not using the instance from the method, it doesn't affect the status of thread safety. As the method only uses the data that is sent to it, it is thread safe.

  3. Either you actually use some instance member in the methods, there is something in the code that could potentially use some instance member, or there is something in the code that makes the tool think that it does.

  1. Static functions have one less argument (the hidden this argument), so theoretically they are somewhat more efficient.
  2. Thread safety has nothing to do with whether your method is static or not. You can make an instance method thread-unsafe just as easily as a static method.
  3. Could you post some of those functions for us to see?
  1. In most situations, you won't notice a performance difference between static and non-static functions. Theoretically, the fact that they cannot be virtual (and don't push the "this" pointer as an argument) make then slightly faster. But again, not something you would usually notice.
  2. Static and thread-safety are not related. If the method was thread-safe before "static", it will be thread-safe after "static".
  3. I have seen this before with some tools. If the additional private methods are used by non-static methods, the code analysis will assume they cannot be made static (even if they do not reference members). If you change the other non-static methods to static (if you can) then it will probably give you the same warning.

Hope that helps, John

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM