C # Learning Diary----static variables and constants

Source: Internet
Author: User
In the previous delegate (Delegate) type, I used the static keyword when I defined the public method, resulting in the inability to access this method in the form of an object. Method name, which we will learn to specify static variables.

Variable:

It is still necessary to understand the meaning of variables before we learn static variables. The program to read, write, operation, and so on, when the need to save a specific value or result in the use of variables, in the view of the user, the variable is used to describe the name of a message, in the variable can be stored in various types of information, such as: the name of the person, the price of tickets and so on; What type of variable is it, and what type is the value stored in the variable. One important principle of using variables is that variables must be defined before they are used.

In C #, the definition of variables and the use of rules and C + + similar, there is not much to say, (so learn C is very important ^_^)

Static statically variable:

A variable declared with the static modifier is called a static variable, and once the class to which the static variable belongs is loaded, it will persist until the end of the program that contains the class. There are two main properties of static:

1. Hide:

A static method or static variable defined in a class belongs to the class itself and not to an object of that class. To invoke a method that is defined as static, you must precede it with the name of the class. (This is also the reason why the public access adornment is not available immediately, which is also the end of the previous article) The instance method must be used by an instance of the class. Instance methods can use non-static members of a class, or they can use static members of a class.

Access rules:

A static method can only access static members of a class and cannot access non-static members of a class;
A non-static method can access a static member of a class, or it can access a non-static member of a class;
Static methods cannot be invoked using an instance, only by using the class name.

For example, the following:

Class person      {public         static int i;   Define a static variable i default value is 0 public         int k=0;         Define a non-static variable K;         public static int SBU ()   //Defines a static method          {             i = 9;  succeeded in assigning a value of static variable             k = 5;  Error, unable to access non-static variable             return k;             Fully static method can only access static variable         } public          int Add ()  //Define an instance method          {              i = 9;    There is no problem assigning a value to a static variable              k = 5;    No problem assigning a value to a non-static variable              return i;              Full instance method to access all type variables          }        }

We instantiate a person an 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 static variables in class              int k = PER.K;//No Problem              per.sbu ();//error, per access to static method              Person.sbu ();  Successfully called              per. Add ();   The person was successfully called              . Add ();  Error, person cannot access instance method                        }

2. Keep the variable content persistent:

Variables stored in the static data area are initialized at the start of the program and are the only one initialized. A total of two variables are stored in static storage: Global variables and static variables, except that, compared to global variables, static can control the visible range of variables, in the final analysis, static is used to hide them.

Write an example to know (this time I wrote in C + +) C # does not allow static in the method:

#include <iostream>    using namespace std;    int main ()  {for     (int i=0;i<4;i++)     {        static int k =0;   Defines a static variable and assigns a value of 0         k++;        cout<<k<<endl;  Output     }         return 0;    }

Results:

If the above code we remove the static at this time k=0, and become a non-static variable, the result will only appear a number 1;

Constant:

A constant is a fixed amount of mass, and the type of a constant from a data type can be any value type or reference type. The declaration of a constant is the constant name and his value that are used in the declaration program. (usage is similar to c) but in C # constants once defined 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 of          static void Main (string[] args)          {              S + = 4;  Error, constants can not change the value of the constant once defined             Console.WriteLine (S);}}  

The above is the C # learning diary----static variables and constants of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

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