简体   繁体   English

我应该将我的私有类方法设为静态吗?

[英]Should I make my private class methods static?

Is there a best practice for making private methods in classes static? 有没有最好的做法让类静态的私有方法? I have a class with a few methods. 我有一个有几种方法的课。 Some of these can easily be made static, since they simply process data. 其中一些很容易变得静态,因为它们只是处理数据。

Should I make them static or just leave them as is? 我应该让它们静止还是只是保持原样? Is this more of a style issue? 这更像是一种风格问题吗? Are there performance considerations? 有性能考虑吗?

Edit: Method can be made static, but should it? 编辑: 方法可以做成静态,但应该吗?

If the methods don't access any of the type's state then they should be static. 如果方法不访问任何类型的状态,那么它们应该是静态的。

Static method calls provide a performance gain over instance methods and the presence of a static method tells future readers of your code that calling this method will create no side effects in the the state of the current instance of the type. 静态方法调用提供了比实例方法更高的性能,并且静态方法的存在告诉未来读者您的代码调用此方法将不会在该类型的当前实例的状态中产生任何副作用。

The performance gain of a static method comes from the fact that the compiler does not have to emit callvirt instructions to call a static method. 静态方法的性能增益来自于编译器不必发出callvirt指令来调用静态方法的事实。 The callvirt instruction is handy for instance calls in that it does a null check prior to calling the method. callvirt指令对于实例调用很方便,因为它在调用方法之前进行null检查。 However, when you call a static methods there is no need to check for null so the compiler is free to emit the faster call instruction which doesn't check for null . 但是,当您调用静态方法时,不需要检查null因此编译器可以自由地发出不检查null的更快的call指令。

As Dan Diplo says, if they can be made static, they should be made static. 正如Dan Diplo所说,如果它们可以变得静止,那么它们应该是静止的。 This is only true for private static methods (which is what you asked about). 这仅适用于私有静态方法(这是您所询问的)。 For public methods, however, they will just confuse users of your class. 但是,对于公共方法,它们只会混淆您班级的用户。 Public methods should be made static only if they will be used without an instance of the class by callers , the performance loss of not making them static be damned. 公共方法只有在没有调用者的类实例的情况下使用时才应该是静态的 ,不会使它们成为静态的性能损失。

I always think if they can be made static (and the compiler will soon let you know if they can't) then they should be made so. 我总是认为如果它们可以被静态化(并且编译器很快会让你知道它们是否可以),那么它们应该是这样的。 See this duplicate question on SO for further discussion. 有关进一步的讨论,请参阅SO上的重复问题

当我确定它们没有任何副作用时,我只会使方法静止。

If a method doesn't use any instance data in the class, it should be static. 如果方法不使用类中的任何实例数据,则它应该是静态的。 That way it's clear that it's independent of the instances, and that you don't have to create an instance to call it. 这样很明显,它独立于实例,并且您不必创建实例来调用它。

Any performance differences should not be a concern. 任何性能差异都不应该成为一个问题。 There is of course a difference in how a static method and an instance method are called, but you will most likely not be able to measure any consistent performance difference. 当然,静态方法和实例方法的调用方式不同,但您很可能无法测量任何一致的性能差异。 The difference is so small that sometimes calling an instance method might actually be faster just because the instructions happens to line up better in the execution. 差异是如此之小,以至于有时调用实例方法可能实际上更快,因为指令恰好在执行中排队。

I agree that all private methods that can be static should be static. 我同意所有可以是静态的私有方法都应该是静态的。 Just remember the performance variation between the 2 are insignificant so don't do this in an attempt to increase performance since early optimization is likely to cause more failures than successes until you know for sure you have a performance issue and profile your application. 请记住,2之间的性能差异是微不足道的,所以不要这样做以试图提高性能,因为早期优化可能会导致更多的失败而不是成功,直到您确定您遇到性能问题并分析您的应用程序。

Think about the static methods and multi-threading. 想想静态方法和多线程。 You must be very careful. 你必须非常小心。 This discusion is like pattern definition: "a solution for a problem in some context.". 这种观点就像模式定义:“在某种情况下解决问题的方法。” You must not say "This should be done like this." 你不能说“这应该这样做”。 It depends of the context. 这取决于具体情况。

如果你有一个静态私有方法,那就好像它可能是一个根本不应该在类中的方法,而应该是一个静态方法库,可以被其他类重用。

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

相关问题 我应该尽可能地让我的方法保持静态吗? - Should I always make my methods static where possible? 我应该让这个类静态吗? - should I make this class static? 用私有构造函数和私有静态方法调用类? - Calling a class with a private constructor and private static methods? 我应该使C#私有方法通用吗? - Should I make C# private methods generic? 我应该使用静态字段还是使用类的实例进行私有访问 - Should I use static fields or private using an instance of the class to access 可以将私有方法放在我的控制器中,还是应该将它们分成某种类型的asp.net mvc辅助类? - Is it okay to put private methods in my controller or should I separate them out into some type of helper class with asp.net mvc? 我是否应该在类库中将方法定义为静态方法以在控制台应用程序中使用 - Should I define methods as static in class library to use in Console Application 我应该将“自定义单元测试声明”类设为非静态吗? - Should I make my Custom Unit Test Assert Class Non-Static? 在类中创建静态方法和字段 - Make static methods and fields in class 我总是把我的静态方法放在类中吗? - Do I have always put my static methods inside the class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM