Static and non-static domains for C #

Source: Internet
Author: User
Tags constructor count

The static field is declared with the static modifier, and the other fields are non-static fields. Static and Non-static fields belong to static and non-static variables in C #, respectively.

If a field is described as static, there is only one copy of static data in memory, regardless of how many instances of the class are established. When the first instance of this class is established, the domain is initialized. When the class is instantiated later, it is no longer initialized, and all instances belonging to this class share a copy.

In contrast, non-static fields each have a separate copy of each instance of the class when it is instantiated.

The following example clearly reflects the difference between the two.

Program Listing 12-1:

Using System;
public class count
{
  static int count;
  int number;
  Public Count () {
      count=count+1;
      Number=count;
  }
  public void Show () {
  Console.WriteLine ("Object{0}:count={1}", Number,count);
 }
Class Test
{public
  static void Main () {
         count a=new count ();
         A.show ();
         Console.WriteLine ();
      Count B=new count ();
         A.show ();
         B.show ();
         Console.WriteLine ();
      Count C=new count ();
         A.show ();
         B.show ();
         C.show ();
}

In the example above, field count in class count is declared static and is shared by instances of all classes. Class is instantiated each time, its value is added to 1, which is implemented in the constructor and can be used to count the number of instances of the class in the system.

The number of domain numbers to hold the current instance. When it is instantiated, the numbering is assigned in the constructor so that the order of the instantiations can be seen.

Method show is used to print out the number of instances of the current class on the screen, as well as the number of individual instances of the class.

The running result of the program should be:

Object1:count=1
——————————
object1:count=2
object2:count=2
——————————
Object1:count=3
Object2.count=3
Object3.count=3

As you can see from the example above, whenever the count values for all instances of a class are the same, they share one data, and the Count field has only one copy. And each instance of the label is different, once instantiated, the label will no longer change.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.