ReSharper喜欢在每个ASP中指出多个函数。NET页面,可以设置为静态。如果我把它们变成静态的,对我有帮助吗?我是否应该将它们设置为静态并将它们移动到实用程序类中?
当前回答
您应该在给定的场景中使用最易读和最直观的方法。
除了在最极端的情况下,性能参数并不是一个好的参数,因为实际发生的唯一事情是将一个额外的参数(this)推入到实例方法的堆栈中。
其他回答
读起来很有趣: http://thecuttingledge.com/?p=57
ReSharper实际上并不是建议您将方法设置为静态的。 你应该问问自己,为什么这个方法在那个类中,而不是在它的签名中出现的类之一…
但ReSharper documentaion是这么说的: http://confluence.jetbrains.net/display/ReSharper/Member+can+be+made+static
它有助于控制名称空间污染。
在我看来,性能、名称空间污染等都是次要的。问问自己什么是合乎逻辑的。该方法在逻辑上操作类型的实例,还是与类型本身相关?如果是后者,则将其设置为静态方法。只有当它与不受您控制的类型相关时,才将其移动到实用程序类中。
Sometimes there are methods which logically act on an instance but don't happen to use any of the instance's state yet. For instance, if you were building a file system and you'd got the concept of a directory, but you hadn't implemented it yet, you could write a property returning the kind of the file system object, and it would always be just "file" - but it's logically related to the instance, and so should be an instance method. This is also important if you want to make the method virtual - your particular implementation may need no state, but derived classes might. (For instance, asking a collection whether or not it's read-only - you may not have implemented a read-only form of that collection yet, but it's clearly a property of the collection itself, not the type.)
您应该在给定的场景中使用最易读和最直观的方法。
除了在最极端的情况下,性能参数并不是一个好的参数,因为实际发生的唯一事情是将一个额外的参数(this)推入到实例方法的堆栈中。
对于类中的复杂逻辑,我发现私有静态方法在创建隔离逻辑时很有用,其中实例输入在方法签名中明确定义,并且不会发生实例副作用。所有输出必须通过返回值或out/ref参数。将复杂的逻辑分解成无副作用的代码块可以提高代码的可读性和开发团队对它的信心。
另一方面,它可能导致类被大量实用方法所污染。通常,逻辑命名、文档和团队编码约定的一致应用程序可以缓解这种情况。
推荐文章
- 我如何使一个方法的返回类型泛型?
- 何时处理CancellationTokenSource?
- 如何获取正在执行的程序集版本?
- AutoMapper vs valueinjector
- 为什么控制台不。Writeline,控制台。在Visual Studio Express中编写工作?
- 什么是.NET程序集?
- 字符串不能识别为有效的日期时间“格式dd/MM/yyyy”
- 函数应该返回空对象还是空对象?
- 如何转换日期时间?将日期时间
- 如何在c#中连接列表?
- 在c#中引用类型变量的“ref”的用途是什么?
- 防止在ASP中缓存。NET MVC中使用属性的特定操作
- 转换为值类型'Int32'失败,因为物化值为空
- c#中有任何连接字符串解析器吗?
- 在Linq中转换int到字符串到实体的问题