简体   繁体   English

低级差异:非静态 class with static method vs. static class with static method

[英]Low-level difference: non-static class with static method vs. static class with static method

I was wondering what are the general benefits (or drawbacks) of using a non-static class with a static method versus a static class with the same static method, other than the fact that I cannot use static methods from a non-static class as extension methods.我想知道使用静态class带有static方法与A static class的一般好处(或缺点)是什么扩展方法。

For example:例如:

class NonStaticClass
{
    public static string GetData()
    {
        return "This was invoked from a non-static class.";
    }
}

Versus this:与此相对:

static class StaticClass
{
    public static string GetData()
    {
        return "This was invoked from a static class.";
    }
}

What are the performance/memory implications of using one method over another?使用一种方法优于另一种方法对性能/内存有何影响?

NOTE: Suppose that I do not need to instantiate the class. My use-case scenario is limited to something like this:注意:假设我不需要实例化 class。我的用例场景仅限于这样的情况:

Console.WriteLine(NonStaticClass.GetData());
Console.WriteLine(StaticClass.GetData());

The main benefit is that if you make the class static, the compiler will make sure that your class will only have static members.主要好处是,如果您将 class 设为 static,编译器将确保您的 class 将只有 static 个成员。

So anyone reading the code, will instantly see the class can't be instantiated, and there is no interaction to be considered with any instances of the class. Because there can't be any.所以任何阅读代码的人都会立即看到 class 无法实例化,并且没有考虑与 class 的任何实例的交互。因为不可能有任何交互。

At the clr level, there is no notion of static .在 clr 级别,没有static的概念。 A static class is both abstract and sealed , which effectively prevents inheritance and instantiation.一个 static class 既abstractsealed ,有效防止了 inheritance 和实例化。

As for performance, I don't see any possiblities for compiler or runtime to optimize one over the other.至于性能,我看不到编译器或运行时有任何可能对一个进行优化。

In this example, I would focus on expressing your intent as clear as possible to the readers.在这个例子中,我会专注于向读者尽可能清楚地表达你的意图。 You can always optimize later.您以后可以随时进行优化。

There are some peculiarity/limitation:有一些特殊性/限制:

  • you can't instantiate static class你不能实例化 static class
  • you can't inherit static class你不能继承 static class

So if you suppose such a behavior for you class (eg helper/util or extension methods container), if you want to limit it's usage - make it static .因此,如果您假设您有这样的行为 class (例如 helper/util 或扩展方法容器),如果您想限制它的使用 - 将其设置为static

There are no benefits or drawbacks.没有好处或缺点。 It's just architectual decision.这只是架构决定。

Use static only classes in cases of providing a function set or function libriary.在提供 function 集或 function 库的情况下,仅使用 static 类。 As you can not instantiate it, but you can use it like.因为你不能实例化它,但你可以像使用它一样。

MyMathLibrary.Plot(...)

This types are usually imply state less behavior.这种类型通常暗示 state 较少的行为。 I repeat, usually .我重复一遍,通常

Use simple classes with static members when you have "normal" type but need somehow a stateless method, like for example:当您具有“正常”类型但不知何故需要无状态方法时,请使用具有 static 个成员的简单类,例如:

public class MyType
{
    .... //some members 

    public static MyType ReadFromXml(XmlReader reader) {}
    public static void SaveToXml(MyType mt) {}
}

There is no a silver bullet, it's just a matter of architect choice.没有灵丹妙药,这只是架构师选择的问题。

Performance implications: basically none.性能影响:基本上没有。

You can create an instance of NonStaticClass , but since it has no non-static overriden methods it's pretty much as useful as saying object obj = new object() , which is to say it's useful for maybe locking and that's about it.您可以创建NonStaticClass的实例,但由于它没有非静态覆盖方法,因此它与说object obj = new object()一样有用,也就是说它可能对锁定有用,仅此而已。 Making the class static prevent someone from new-ing it, but it's not like that does any real harm .制作 class static 可以防止有人对其进行 new-ing,但这不会造成任何真正的伤害

It's more of a self documenting way of saying there's no reason to create an instance of the class.它更像是一种自我记录的方式,表明没有理由创建 class 的实例。

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

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