简体   繁体   English

在c#中创建共享静态字段

[英]Creating a shared static field in c#

The question that I have is in regards to creating a shared field that is Static within an instance class. 我的问题是在实例类中创建静态的共享字段。 The code that I am going to display works perfectly fine. 我要展示的代码完美无缺。 What I am really wanting to grasp is the understanding behind the logic. 我真正想要掌握的是逻辑背后的理解。 Here is a code snippet which is of a very basic class and a method which creates objects from this class and displays to the screen how many objects have been created. 这是一个代码片段,它是一个非常基本的类,它是一个从这个类创建对象并在屏幕上显示已创建了多少个对象的方法。

public class Counter
{
private int value;
private static int ValueCounter = 0;

public Counter()
{
    value = ValueCounter;
    ValueCounter++;
}

public static int objectCount()
{
    return ValueCounter;
}
}

Above is a very basic but working class. 以上是一个非常基本的工人阶级。 I am trying to keep it as simple and as basic as possible so that I get the understanding of it. 我试图保持它尽可能简单和基本,以便我理解它。

Below is now the code snippet that creates instances of the class and displays to the screen how many instances have been created. 下面是创建类实例的代码片段,并在屏幕上显示已创建的实例数。

private static void showObjectCounter()
    {
        Counter val1 = new Counter();
        Counter val2 = new Counter();
        Counter val3 = new Counter();
        Counter val4 = new Counter();

        int totalValue = Counter.objectCount();
        Console.WriteLine("Total objects created = {0}", totalValue);
    }

Now this is my question regarding this matter. 现在这是关于这件事的问题。 When a new object is created the value field is independent of each instance hence each object gets there own copy. 创建新对象时,value字段独立于每个实例,因此每个对象都有自己的副本。 However the static field is a shared field and incremented with each new creation of an object. 但是,静态字段是共享字段,并随着对象的每次新创建而递增。 I was wondering if this increment value is saved after each creation of the new object ie one object is created value is incremented to 1, then another object is created and the last value of the increment was at 1 so now it is incremented to 2 and so on for each new object creation. 我想知道在每次创建新对象后是否保存了这个增量值,即创建了一个对象,值增加到1,然后创建另一个对象,增量的最后一个值为1,所以现在它增加到2并且等等,为每个新对象创建。

Or does each new object that is created get incremented to 1 and then are all added together to find the total value of the objects created ? 或者,每个创建的新对象是否增加到1,然后全部加在一起以查找创建的对象的总值?

It is a very basic and simple question but Iam fighting with myself over which is ryt or wrong prob because of the time of nyt that it is. 这是一个非常基本和简单的问题,但是由于时间的推移,我与自己争论是错误或错误的概率。 Anyhow if anyone has a simple answer to a very simple question then please feel free to share it. 无论如何,如果有人对一个非常简单的问题有一个简单的答案,那么请随时分享。

Thanks. 谢谢。

It's really a shared value, so every time a constructor runs, the value is incremented by 1 regardless of when the variable is accessed thereafter. 它实际上是一个共享值,因此每次构造函数运行时,无论何时访问该变量,该值都会递增1。 You could verify this yourself: 您可以自己验证:

private static void showObjectCounter()
{
    Counter val1 = new Counter();
    Console.WriteLine("Total objects created = {0}", Counter.objectCount());
    Counter val2 = new Counter();
    Console.WriteLine("Total objects created = {0}", Counter.objectCount());
    Counter val3 = new Counter();
    Console.WriteLine("Total objects created = {0}", Counter.objectCount());
    Counter val4 = new Counter();
    Console.WriteLine("Total objects created = {0}", Counter.objectCount());
}

You'll see that value changes every time. 你会发现价值每次都在变化。

You might be helped by using some conventional naming and coding style. 使用一些传统的命名和编码风格可能会对您有所帮助。

  • Use this for instance variables this用作实例变量
  • Use C# Property instead of getter/setter method 使用C#属性而不是getter / setter方法
  • Don't use this for static variables 不要将this用于静态变量
  • Slightly more descriptive naming 略微更具描述性的命名

Sample: 样品:

public class Counter
{
    public int Count { get; }

    private static int instanceCounter = 0;
    public static int InstanceCount
    {
        get
        {
            return instanceCounter;
        }
    }

    public Counter()
    {
        this.Count = instanceCounter;
        instanceCounter++;
    }
}

In your example, there is only one physical location in memory that corresponds to the ValueCounter variable. 在您的示例中,内存中只有一个物理位置对应于ValueCounter变量。

By contrast, the non-static value field is stored in each instance's object data. 相反,非静态value字段存储在每个实例的对象数据中。

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

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