gpt4 book ai didi

针对大量类实例的 C# 优化

转载 作者:行者123 更新时间:2023-12-03 17:10:56 24 4
gpt4 key购买 nike


我的工作使用了大量的类实例。
对于内存优化,我想知道使用静态方法是否会比简单函数更好。
感谢您就管理大量对象的优化提出任何建议。

最佳答案

没有区别

public class Foo
{
public Bar bar;
public Bar baz;

public bool Qux()
{
return this.bar != null;
}
}

public class Foo
{
public Bar bar;
public Bar baz;

public static bool Qux(Foo foo)
{
return foo.bar != null;
}
}

就单个实例的内存消耗而言。方法消耗每个类的内存,而不是每个实例。

当然,您可以通过不创建实例来节省内存。因此,如果您的静态方法不需要实例,请使用静态方法。

关于针对大量类实例的 C# 优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3700666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com