同一应用程序中的多个静态变量范围?

发布于 2024-12-07 10:37:39 字数 550 浏览 0 评论 0原文

在同一应用程序中设置多个静态作用域的最佳方法是什么? 我有用作访问数组的包装器的结构。

下面是一个示例:

class FooClass{
   static int[] BarArray;
}

struct FooStruct{
    public int BarArrayIndex;

    public int BarArrayValue { 
      get { return FooClass.BarArray[BarArrayIndex]; } 
      set { FooClass.BarArray[BarArrayIndex] = value; }
    }
}

出于性能原因,我不想在 FooStruct 的每个实例中存储对 BarArray 的引用,因此我将数组声明为静态。 但是,将来我可能必须同时使用多个不同的 BarArray(其中结构的不同实例应指向不同的数组)。有没有一种方法可以实现这一目标,而无需在结构的每个实例中存储额外的引用,也不使用静态变量? 如果不是,那么使用多个静态实例同时使整个应用程序对最终用户来说就像“一个应用程序”的最佳方法是什么?

What is the best way to set up multiple static scopes within the same application?
I have structs that serve as wrappers over accessing an array.

Here's an example:

class FooClass{
   static int[] BarArray;
}

struct FooStruct{
    public int BarArrayIndex;

    public int BarArrayValue { 
      get { return FooClass.BarArray[BarArrayIndex]; } 
      set { FooClass.BarArray[BarArrayIndex] = value; }
    }
}

For performance reasons, I don't want to store a reference to BarArray in every instance of FooStruct, hence I declared the array static.
However, it's possible that in the future I'll have to work with multiple different BarArrays at the same time (where different instances of the struct should point into different arrays). Is there a way to achieve that without having to store an additional reference in every instance of the structs and not using a static variable neither?
If not, what's the best way to use multiple static instances while making the whole application feel as "one application" for the end-user?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

著墨染雨君画夕 2024-12-14 10:37:39

您似乎认为持有对数组的引用意味着复制数组..即您的结构的每个实例都将包含数组的副本?事实并非如此。所有结构将包含对数组的引用......一个指针。内存中只会存在该数组的一个实例。我不确定这是否能为您带来任何性能分数。

You seem to think that holding a reference to an array means copying the array .. ie that each instance of your struct would contain a copy of the array? This is not the case. All the struct would contain is a reference to the array ... a pointer. There would only exist one instance of the array in memory. I'm not sure this is gaining you any performance points.

我不咬妳我踢妳 2024-12-14 10:37:39

你不能。静态的要点是在整个应用程序中拥有一个实例。

请查看依赖注入。它应该完美地满足您的用例,并且是处理此类问题的首选方法。

You can't. The point of static is to have one instance over the whole application.

Have a look at Dependency Injection instead. It should fulfill your usecase perfectly fine and is the prefered way of handling such a problem.

魔法唧唧 2024-12-14 10:37:39

这不仅仅是记忆。每次我创建一个新实例或复制它时
(将其传递给方法等)它也会增加一些 cpu 时间开销。
这是我想尽量减少的主要事情

然后使它们成为类对象。然后你只需要传递一个引用,你就可以将引用添加到数组中而不会受到惩罚。 (不,在堆上使用 1M 小对象不是性能问题)。

但我严重怀疑复制小结构是由分析器挑选出来的。听起来您正在猜测瓶颈在哪里。

It's not only memory. Every time I create a new instance or copy it
(pass it to a method etc.) it'll add some cpu time overhead as well.
That's the main thing I'd like to minimize

Then make them class objects. Then you only have to pass a reference around and you can add a ref to the array without penalty. (And No, using 1M small objects on the heap is not a perfromance issue).

But I seriously doubt that copying small structs was singled out by a profiler. It sounds like you're guessing where the bottleneck is.

静若繁花 2024-12-14 10:37:39
static class FooClass{
    private static int[][] barArrays;

    public static int[] getBarArray(int instanceIndex)
    {
        return barArrays[instanceIndex];
    }
}

struct FooStruct{
    private int instanceIndex;
    public int BarArrayIndex;

    public int BarArrayValue { 
        get { return FooClass.getBarArray[instanceIndex][BarArrayIndex]; } 
        set { FooClass.getBarArray[instanceIndex][BarArrayIndex] = value; }
    }
}

这是单例模式的概括。

顺便说一句,每个 FooStruct 实例保存 FooClass 的公共实例的性能损失绝对是微不足道的。

static class FooClass{
    private static int[][] barArrays;

    public static int[] getBarArray(int instanceIndex)
    {
        return barArrays[instanceIndex];
    }
}

struct FooStruct{
    private int instanceIndex;
    public int BarArrayIndex;

    public int BarArrayValue { 
        get { return FooClass.getBarArray[instanceIndex][BarArrayIndex]; } 
        set { FooClass.getBarArray[instanceIndex][BarArrayIndex] = value; }
    }
}

This is a generalization of the Singleton pattern.

By the way, the performance penalty for each instance of FooStruct to hold on to a common instance of FooClass is absolutely trivial.

以为你会在 2024-12-14 10:37:39

此时您能做的最好的事情就是向 FooClass 添加一个工厂方法,负责返回 BarArray 的实例。

class FooClass {
   int[] GetBarArray() {
   }
}

现在,实现此方法以返回静态对象。如果将来您决定更改创建数组的策略,只需重新实现工厂方法即可。

The best you could do at this moment is to add a factory method to the FooClass, responsible for returning an instance of the BarArray.

class FooClass {
   int[] GetBarArray() {
   }
}

For now, implement this method to return a static object. If somewhere in future you decide to change the policy of creating the array, you just reimplement the factory method.

不甘平庸 2024-12-14 10:37:39

如果您只想拥有多个静态变量,请为每个上下文使用新的 AppDomain。
但我不确定这是否适合您的问题(请参阅其他答案)。

编辑:
教程和有用的
http://msdn.microsoft.com/en-us/library/ system.appdomain.aspx
http://www.beansoftware.com/NET-Tutorials/Application-Domain。 .aspx
我不了解应用程序域

If you just want to have multiple static Variables use a new AppDomain for every Context.
But i'm not sure if this is the right soultion for your Problem (See other Answers).

EDIT:
Tutorials and Helpful
http://msdn.microsoft.com/en-us/library/system.appdomain.aspx
http://www.beansoftware.com/NET-Tutorials/Application-Domain.aspx
I don't understand Application Domains

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文