C # static and instance members

Source: Internet
Author: User
ArticleDirectory
    • 7.5 static and instance members
7.5 static and instance members

Add the keyword static before the type of the class member or the return value type to define the member as a static member ). Constants or types are implicitly declared as static members. Other members without static modification are instance members or non-static members. Static members belong to the class and are shared by all instances of the class. instance members belong to objects (class instances), and each object has different copies of instance members.

Static members have the following features:

-Static members must be referenced by class names using the. Operator, rather than objects.

-A static field only identifies one storage location. No matter how many instances of a class are created, its static fields only occupy the same region in the memory.

-Static function members (methods, attributes, events, operators, or constructors) cannot act on a specific instance. Such function members cannot directly use instance members and must be referenced by class names.

Instance members have the following features:

-Instance members must be referenced by object names using the. Operator, rather than class names.

-The instance field of the class belongs to all instances of the class. Each instance that creates a class opens a zone for the instance field in memory. Each instance of the class contains copies of all instance fields of the class.

-Instance function members (methods, attributes, indexers, instance constructors, or destructor) Act on a given instance of the class.CodeThe body can directly reference static and instance members of the class.

In fact, the writeline and other methods of the console class that we used in the previous chapters are static methods and are referenced through the class name console.

Understand the following code and how to use static and instance members:

Class Test

{

Int X; // instance Field

Static int y; // static field

Void F () // instance method

{

X = 1; // correct: instance fields can be directly referenced in the instance method

Y = 1; // correct: static fields can be directly referenced in the instance method

}

Static void g () // static method

{

X = 1; // error: the instance field cannot be directly referenced in the static method.

Y = 1; // correct: the static field can be directly referenced by the static method.

}

Static void main () // static method

{

Test T = new test (); // create an object

T. X = 1; // correct: reference the instance field with the object

T. y = 1; // error: the object name cannot be used to reference static fields.

Test. x = 1; // error: the instance field cannot be referenced by the class name.

Test. y = 1; // correct: Reference static fields with class names

T. F (); // correct: Call the instance method with an object

T. G (); // error: the static method cannot be called with the object name.

Test. F (); // error: the instance method cannot be called by Class Name

Test. G (); // correct: Call the static method with the class name

}

}

Note that in the above Code, main is also a static method of the test class. You can directly use static members in it, instead of using the class name for reference.

The followingProgramDemonstrate the nature of static fields:

// Staticmember. CS

// Example of static Fields

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 ();

}

}

The output result is:

Object1: Count = 1

-----------------

Object1: Count = 2

Object2: Count = 2

-----------------

Object1: Count = 3

Object2: Count = 3

Object3: Count = 3

From the above example, we can see that, at any time, the static field Count value of all instances of the class is the same, and an instance changes its value, the values of other instances also change, indicating that all instances use the same count field. The value of the member field number of each instance is different, it cannot be changed by other instantiation methods.

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.