C # static field initializers cannot reference non-static fields, methods, or properties

Source: Internet
Author: User

Issue: Field or property problem field initializers cannot reference non-static fields, methods

Why is the field defined in the class not available because of an error in the following code?

     Public string Text = test ();//Hint field or property problem field initializer cannot refer to non-static field, method     protectedvoid Page_ Load (object  sender, EventArgs e)    {     }    publicstring  test ()    return"";}

Can be called by property:

      Public string Get return test ();} }    protectedvoid Page_Load (object  sender, EventArgs e)    {      

C # Specifies that only attributes or variables can be defined within a class and initialized, and cannot be directly referenced by variables.

A field is called before the class instance is initialized

The initialization of a field in C # precedes the constructor.

String y = "asdf" + x; is actually y = "asdf" + this.x;

Because this is not initialized at this point, the compilation will error.

Property memory and variable memory are allocated in different times.
Attributes are allocated at the time of use, and variables are assigned when the class is initialized.

Use the static modifier to declare static members that belong to the type itself rather than to a particular object can be used for classes, fields, methods, properties, operators, events, and constructors, but not for types other than indexers, destructors, or classes

Static global variables

definition : Before a global variable, the variable is defined as a static global variable, plus the keyword static.

features :
A, the variable allocates memory in the global data area.
B, initialization: If it is not explicitly initialized, it is implicitly initialized to 0.

Static local Variables

definition : Static local variables are defined when a local variable is preceded by a static keyword.

features :
A, the variable allocates memory in the global data area.
B, initialization: If it is not explicitly initialized, it is implicitly initialized to 0.
C, it always resides in the global data area until the program finishes running. But its scope is a local scope, and when the function or block of statements that defines it ends, its scope ends.


Static data members
features :
A, memory allocation: In the program's global data area allocation.
B, initialization and definition:
A, static data members are defined to allocate space, so they cannot be defined in a class declaration.
B, in order to avoid multiple use of the class in the source file, the duplicate definition, where it cannot be in the header file of the class
Defined.
c, static data members are required to be present at the beginning of a program, so the best place to initialize it is implemented inside the class.
C, characteristics
A, the effect on the Public,protected,private keyword it is like a regular data member,
b, because its space is allocated in the global data area and belongs to all objects of this class, it does not belong to a particular class object, and its scope is visible when no class object is produced, that is, we can manipulate it without producing an instance of the class.

D. Form of Access
A, class object name. static data member name

E, a static data member, primarily used on properties owned by all instances of the class. For example, for a deposit class, the account number is different relative to each instance, but the interest per instance is the same. Therefore, interest should be set as a static data member of the deposit class. There are two benefits, first, regardless of how many deposit class objects are defined, the interest data members share the memory allocated in the global zone, thus saving storage space. Second, once the interest needs to be changed, the interest in all the deposit classes changes all the time, because they are actually sharing one thing.

Static member functions
features :
A, A static member function is associated with a class and is not associated with the object of the class.
B, static member functions cannot access non-static data members. The reason is simple: non-static data members belong to a particular class instance.
function :
Primarily used for operations on static data members.

invocation Form :
A, class object name. static member function name ()

C # static field initializers cannot reference non-static fields, methods, or properties

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.