简体   繁体   English

我总是把我的静态方法放在类中吗?

[英]Do I have always put my static methods inside the class?

I have something like this:我有这样的事情:

Now i need to write some static methods too which is context specific, like when I'm using the objects in WinForms I don't need html related static methods.现在我也需要编写一些特定于上下文的静态方法,比如当我在 WinForms 中使用对象时,我不需要与 html 相关的静态方法。 Is there a way to describe static methods else where other than nesting them within the object class like I can do with the Extension methods?除了像扩展方法那样将它们嵌套在对象类中之外,还有其他方法可以描述静态方法吗?

Now i need to write some static methods too which is context specific, like when I'm using the objects in WinForms I don't need html related static methods.现在我也需要编写一些特定于上下文的静态方法,比如当我在 WinForms 中使用对象时,我不需要与 html 相关的静态方法。 Is there a way to describe static methods else where other than nesting them within the object class like I can do with the Extension methods?除了像扩展方法那样将它们嵌套在对象类中之外,还有其他方法可以描述静态方法吗?

You can place them in their own class, separate from the "object" on which they're acting.您可以将它们放在它们自己的类中,与它们所作用的“对象”分开。 This is effectively all an extension method does - it's a static method in a separate class.这实际上是扩展方法所做的一切——它是一个单独类中的静态方法。

You can put extension methods in any number of static classes, there's nothing particularly special about your PSVitaExtentions class.您可以将扩展方法放在任意数量的静态类中,您的PSVitaExtentions类没有什么特别之处。 The extension methods are callable as long as you have pulled in their namespace, so as @griegs says it may be wise to put your second set of extension methods in a different namespace.只要您拉入它们的命名空间,扩展方法就是可调用的,因此正如@griegs 所说,将您的第二组扩展方法放在不同的命名空间中可能是明智的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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