The static usage in C # is detailed

Source: Internet
Author: User

C # static full usage collection and collation

Full usage of static

To understand the static, you must first understand the other relative keywords, many people may not know that there is a keyword, that is, auto, in fact, we usually declare the use of static modified variables, are auto, because it is the default, Just as short and long always default to int; we usually declare a variable:

int A;

string S;

is actually:

auto int A;

Auto string s;

and the declaration of the static variable is:

static int A;

static string S;

This seems to be more conducive to understanding the auto and static is a pair of keywords, like private,protected,public;

For the static does not understand, in fact, it is not understanding of auto, because it is more general, some things you use every day, but not necessarily on behalf of you really understand it; the meaning of auto is to control the life cycle of the variable by the program, usually the variable is allocated when it enters its scope, is released from its scope; the static is not auto, the variable is allocated when the program is initialized, until the program exits before it is released; the static is assigned to release variables according to the lifecycle of the program, rather than the variable's own life cycle; So, examples like this:

void func()
...{
int a;
static int b;
}
Each time the function is called, the variable A is new, because it is allocated when it enters the function body, it is released when it exits the function body, so multiple threads calling the function have their own independent variable a because it is always reassigned; and variable B doesn't matter whether you use the function or not, is assigned when the program is initialized, or when it is first executed to its declaration (different compilers may be different), so when multiple threads call the function, they always access the same variable B, which must be noted in multithreaded programming!

1. Static members of a class:

class A
...{
private:
static int s_;
};

It must be initialized in CPP:

int a::s_ = 0;//Note that there is no static modification here!

A static member of a class is a shared member of all instances of the class, that is, a global variable within the scope of the class. It can also be understood to be a global variable named A::s_, except that it has a class security attribute; it's simple because it's allocated at the time of the program initialization, so it's shared only once, so it's common. ;

The static members of a class must be initialized, and the same is true. Because it is allocated at the time of initialization of the program, it must have initialization, the class is only declared, in the CPP is initialized, you can put a breakpoint on the initialization code, before the program executes main the first statement will go there first If your static member is a class, then it calls to its constructor;

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.