代码分析 C# .NET CA1822

发布于 2024-11-16 23:57:12 字数 1121 浏览 2 评论 0原文

我运行代码分析并收到以下消息:

CA1822:Microsoft.Performance:“this”参数(或“Me”) Visual Basic) 的“CreateIntervalString(TimeSpan)”从未使用过。标记 将成员设为静态(或在 Visual Basic 中为共享)或在中使用 'this'/'Me' 方法主体或至少一个属性访问器(如果适用)。

我的代码是:

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

据我了解,因为 CreateIntervalString 函数不使用类的任何成员,并且仅使用 timeSpan 输入,VisualStudio 建议我将其标记为静态。

我的问题:

  1. 为什么当我将其标记为静态时,性能会提高?
  2. 我的函数是应该是线程安全的库的一部分,将方法标记为静态是否可以防止这种情况发生?
  3. 我有其他私有函数,它们仅使用其输入,并且不使用该类的任何其他成员,但我没有得到相同的错误。

多谢!

示例:

以下方法提供错误:

    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:

  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);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

顾挽 2024-11-23 23:57:12

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.

分开我的手 2024-11-23 23:57:12
  1. 性能没有提高(以任何重要的方式),但代码变得更清晰。该方法不会给人留下使用实例的印象,并且您可以使用该方法而无需创建类的实例。

  2. 由于您没有使用方法中的实例,因此它不会影响线程安全的状态。由于该方法仅使用发送给它的数据,因此它是线程安全的。

  3. 实际上在方法中使用了某些实例成员,代码中的某些内容可能会使用某些实例成员,或者代码中的某些内容使工具认为它确实使用了某些实例成员。

  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.

转角预定愛 2024-11-23 23:57:12
  1. 静态函数少了一个参数(隐藏的 this 参数),因此理论上它们的效率更高。
  2. 线程安全与您的方法是否静态无关。您可以像静态方法一样轻松地使实例方法成为线程不安全的。
  3. 您能否发布其中一些功能供我们查看?
  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?
錯遇了你 2024-11-23 23:57:12
  1. 在大多数情况下,您不会注意到静态函数和非静态函数之间的性能差异。从理论上讲,它们不能是虚拟的(并且不将“this”指针作为参数推送)这一事实使得速度稍微快一些。但同样,这不是你通常会注意到的事情。
  2. 静态和线程安全不相关。如果该方法在“static”之前是线程安全的,那么在“static”之后它也将是线程安全的。
  3. 我以前用一些工具见过这个。如果非静态方法使用其他私有方法,则代码分析将假定它们不能变为静态(即使它们不引用成员)。如果您将其他非静态方法更改为静态(如果可以的话),那么它可能会给您相同的警告。

希望有帮助,
约翰

  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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文