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:
- Why when I mark it as static, the performance is improved?
- My function is part of library that should be thread-safe, does marking method as static prevent this?
- 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)
开发者_JAVA百科{
// 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.
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.
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.
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.
- Static functions have one less argument (the hidden
this
argument), so theoretically they are somewhat more efficient. - 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.
- Could you post some of those functions for us to see?
- 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.
- Static and thread-safety are not related. If the method was thread-safe before "static", it will be thread-safe after "static".
- 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
精彩评论