C # Learning diary 20 ---- static variables and constants

Source: Internet
Author: User

C # Learning diary 20 ---- static variables and constants

 

Variable:

Before learning static variables, we still need to understand the significance of variables. The program needs to read, write, and perform operations on the data. It needs to use variables when it needs to save specific values or results. In the user's opinion, A variable is a name used to describe a piece of information. It can store various types of information, such as the person's name and ticket price. In the computer's view, a variable represents a storage address, what type is a variable, and what type is the value stored in the variable. An important principle for using variables is that variables must be defined first and then used.

Variable definition and usage rules in C # are similar to those in C/C ++. I will not talk about them here (so it is very important to learn C well)

 

Static variables:

A variable declared with a static modifier is called a static variable. Once the class to which the static variable belongs is loaded, it will exist until the program containing this class ends. Static has two main properties:

1. Hide:

Static methods or static variables defined in the class belong to the class itself, rather than an object of that class. To call a method defined as static, you must add the name of this class before it. (The real-time modification is not feasible for public access, which is also the reason at the end of the previous Article) the instance method must be used through the instance of the class. You can use non-static members of a class or static members of a class.

Access rules:

Static methods can only be static members of the primary class, but not non-static members of the primary class;
The non-static method can be a static member of the category or a non-static member of the category;
Static methods cannot be called by instances, but by class names.

For example:

Class person {public static int I; // defines a static variable I. The default value is 0 public int k = 0; // defines a non-static variable k; public static int sbu () // define a static method {I = 9; // The static variable k = 5 is successfully assigned; // The non-static variable return k cannot be accessed due to an error; // In summary, static methods can only access static variables} public int Add () // define an instance method {I = 9; // no problem in assigning values to static variables k = 5; // no problem in assigning values to non-static variables return I; // In summary, the instance method can access all types of variables }}


We instantiate a person object to access the method:

 

Static void Main (string [] args) {person per = new person (); // instantiate an object per int I = per. i; // error. per cannot access the static variable int k = per. k; // No problem per. sbu (); // error. per cannot access the static method person. sbu (); // call per. add (); // The person is successfully called. add (); // error. The person cannot access the instance}


2. Keep the variable content persistent:

 

Variables stored in the static data area will be initialized at the beginning of the program, which is also the only initialization. There are two types of variables stored in the static storage area: global variables and static variables, but compared with global variables, static can control the visible range of variables. static is used to hide the variables.

Write an example and you will know (I wrote it in C ++ this time) C # static is not allowed in the method:

# Include
 
  
Using namespace std; int main () {for (int I = 0; I <4; I ++) {static int k = 0; // define a static variable and assign a value of 0 k ++; cout <
  
   

Result:

 

If the above Code removes static, then k = 0; becomes a non-static variable, only one number 1 is displayed;

 

 

Constant:

A constant is a constant volume. From the data type, the constant type can be any value type or reference type. The declaration of a constant is to declare the constant name and its value used in the program. (Similar to C) but once defined in C #, a constant cannot change its value.

 

Using System; using System. collections. generic; using System. linq; using System. text; namespace demo {class Program {const int S = 9; // define a constant S and assign a value to static void Main (string [] args) {S + = 4; // if an error occurs, once a constant is defined, the constant value cannot be changed. writeLine (S );}}}

 

 

 

 

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