代码分析 C# .NET CA1822
我运行代码分析并收到以下消息:
CA1822:Microsoft.Performance:“this”参数(或“Me”) Visual Basic) 的“CreateIntervalString(TimeSpan)”从未使用过。标记 将成员设为静态(或在 Visual Basic 中为共享)或在中使用 'this'/'Me' 方法主体或至少一个属性访问器(如果适用)。
我的代码是:
private string CreateIntervalString(TimeSpan timeSpan)
{
return timeSpan.ToString();
}
据我了解,因为 CreateIntervalString
函数不使用类的任何成员,并且仅使用 timeSpan 输入,VisualStudio 建议我将其标记为静态。
我的问题:
- 为什么当我将其标记为静态时,性能会提高?
- 我的函数是应该是线程安全的库的一部分,将方法标记为静态是否可以防止这种情况发生?
- 我有其他私有函数,它们仅使用其输入,并且不使用该类的任何其他成员,但我没有得到相同的错误。
多谢!
示例:
以下方法提供错误:
private string CreateIntervalString(TimeSpan timeSpan)
{
return timeSpan.ToString();
}
而以下方法则不提供错误:
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);
}
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)
{
// the years,monthe,days,hours,minutes and secondes found by the dateTimeString input
return new DateTime(years, months, days, hours, minutes, secondes);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
MSDN 站点 http://msdn.microsoft.com/en-us/library/ms245046.aspx 给出了性能方面的答案
如果该方法未标记为静态,则运行时将检查当前对象 (this) 是否为 null。在大多数情况下,几乎没有可观察到的差异,这是事实,但如果每秒调用数百万次的方法可以通过静态化来获得这种收益,那么它可能是值得的。
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.
this
参数),因此理论上它们的效率更高。this
argument), so theoretically they are somewhat more efficient.希望有帮助,
约翰
Hope that helps,
John