Static members and non-static members in C #

Source: Internet
Author: User

If a member in a class is declared as static, the member is called a quiescent member. The members in the class are either static or non-static. Generally, static members belong to the class, and non-static members are instances of the class-objects.

The following sample code demonstrates how to declare static and non-static members.

Program Listing 10-5:

Using System;
Class Test
  int x;
  static int y;
     void F () {
     x=1;//correct, equivalent to this.x=1
     y=1//correct, equivalent to Test.y=1
  static void G () {
  x=1;//error, no access to this.x
  Y=1//correct, equivalent to Test.y=1
  static void Main () {
  Test t=new test ();
  T.x=1;  Correct
  T.y=1;  Error, static member Test.x=1 cannot be accessed in an instance of a class
  ;//Error, non-static member Test.y=1 cannot be accessed by class

A non-static member of a class belongs to an instance of a class, and each instance of a class creates an area in memory for non-static members. The static members of the class are owned by the class and are shared by all instances of the class. No matter how many replicas this class creates, a static member occupies only one area of memory.

Related Article

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: 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.