Local static variables in C #

Source: Internet
Author: User
Tags variable scope

In fact, there is nothing to discuss, C # does not support local static variables.

But I still think about why C # does not support local static variables, the following are personal thoughts.

C + + and c support local static variables, that is, the declaration of a static variable inside a function, which is specific to the following:

    • A static local variable is defined within a function, but not as an automatic variable, when it is called and disappears when the function is exited. Static local variables always exist, that is, their lifetime is the entire program life cycle
    • The lifetime of a static local variable is the entire source program, but its scope is still the same as the automatic variable, that is, the variable can only be used within the function that defines it. After exiting the function, although the variable continues to exist, it cannot be used
    • A static variable can be assigned to a variable at the same time as the initialization statement, which executes only once when the function is first invoked.
    • If there is no initialization statement, the compiler initializes the variable with the default value of that type

In C #, however, it is not possible to declare this static variable that exists inside the function, and a compilation error may occur, for the following reasons:

Both C + + and C are not fully object-oriented programming languages, that is, there are many functions that do not belong to instances of any class or class, and there are three ways to do this if a variable of a function has the attribute "retain the value of the last function call":

    • Declaring global variables
    • Declaring global static variables
    • Declaring local static variables

Global variables and global static variables can implement a variable "retain the value of the last function call" attribute, but the scope of the two variables is larger, the global variable scope is the entire source program, the scope of the global static variable is the CPP file where the variable is declared, the scope of the variable is too large to create a security risk, Because the variable can also be called by other global functions or even member functions of the class, using local static variables is the best choice. It ensures that the variable has the function of "memory" and has a good limitation on the scope or the visibility of the variable.

In contrast, C # is a fully object-oriented language, and any variable or function should belong to an instance of a class or class, when the function of a local static variable in C + + can be implemented by a static member variable of the class, except that the minimum scope of the variable's visibility is the inner (private static variable) of the class, not C + + Inside the function of the local static variable, slightly enlarging the scope of the variable.

But I think this shortcoming is acceptable, otherwise or destroy the concept of "class" completeness, in the language of object-oriented programming, class or class instance is the most basic constituent unit, any member of the class should be visible inside the class, if there is a local static variable concept exists, then it is like your home has a car, But you have no right to open the same.

Take this opportunity to summarize the application of the static modifier in C #, the scenario you can use:

    • A decorated class, static class, implicitly sealed by a static class, and whose members must all appear declared as static
    • Decorated class members: Members inside classes such as member variables, member methods, and so on
    • modifying ReadOnly Members

Scenarios that you cannot use:

    • Local variable inside the method or property, i.e. no local static variable exists
    • interface declaration, that is, there is no static interface
    • Interface member is famous, that is, there is no static member inside the interface
    • Const member, dull const member has properties of static member, but cannot be displayed using the static modifier

Local static variables in C #

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.