Asp.net (C #) Static keyword usage Summary + sample code

Source: Internet
Author: User
Static variables

When writing a class, we are actually describing the attributes and behaviors of the object, but not generating a substantive object. Only through the New Keyword will the object be generated, at this time, the system will allocate memory space to the object, and its method can be used for external calls.

Sometimes, we hope that, no matter whether an object is generated or no matter how many objects are generated, some specific data has only one copy in the memory space. For example, all Chinese people have country names, every Chinese person shares the country name and does not have to assign a variable for the country name of the Code to every Chinese person's instance object. Example:

Protected void page_load (Object sender, eventargs E)
{
// The format of "class name. member" is used directly, and the format of "Object Name. member" cannot be used. This is different from that of Java.
Response. Write (Chinese. Country );
}

Class Chinese
{
Public static string Country = "China ";
Public void getcountry ()
{
// The member methods in the class can also directly access static member variables
Httpcontext. Current. response. Write ("Hello! "+ Country );
}
}

Note that the variables in any method body cannot be declared as static, as shown below:

Public void getcountry ()
{
// The member methods in the class can also directly access static member variables
Httpcontext. Current. response. Write ("Hello! "+ Country );
Static int I = 1;
}

Static Method

 

Sometimes we want to call a method without creating an object. In other words, this method does not have to be bound with an object. To achieve this effect, you only need to add the static keyword before the method defined in the class. We call this method a static member method, you can also access this static method like other static methods in the non-static member method of the class. For example:

Protected void page_load (Object sender, eventargs E)
{
Chinese. getcountry ();
}

Class Chinese
{
Public static void getcountry ()
{
Httpcontext. Current. response. Write ("Hello! China ");
}
}

Note: In a static method, only other static members (including variables and methods) of the same type can be called directly, rather than non-static members in the category. This is because for non-static methods and variables, you need to create an instance object for the class before using it, and the static method does not need to create any objects before using it.

Const and static readonly are indeed very similar: access through the class name rather than the object name, read-only in the program, and so on. In most cases, they can be mixed.
The essential difference between the two is that the const value is determined during compilation, so it can only be specified through a constant expression during declaration. Static readonly calculates its value during running, so it can be assigned a value through a static constructor.

Auto

To understand static, you must first understand another relative keyword. Many people may not know this keyword, that is, Auto. In fact, we usually declare variables without static modification, all are auto, because it is default, just as short and long are always int by default; we usually declare a variable:
Int;
String S;
Actually:
Auto int;
Auto string S;
The static variable declaration is:
Static int;
Static string S;
This seems to be more conducive to understanding that auto and static are paired keywords, just like private, protected, and public;
I don't understand static, but I don't understand auto because it is more general. Some things you use every day, but it doesn't necessarily mean you really understand it; auto means that the program automatically controls the life cycle of the variable. It usually means that the variable is allocated when it enters its scope and is released when it leaves its scope; static is not auto, and variables are allocated during program initialization until the program exits. Static is assigned to release variables according to the life cycle of the program, instead of the variable's own Lifecycle

 

Add a test code written by myself:

Test Page code:

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace statickeyword
{
Class Program
{
Static void main (string [] ARGs)
{
// Call (static) static variable
// The static variable in (class) Class statickeyword can be called directly in (class) Class program through (class name. static variable name) statickeyword. Country
Console. writeline ("this variable is" + statickeyword. staticvariable );
// Non-static method + non-static variables. Before using non-static methods, you must convert them into class objects.
Statickeyword skw1 = new statickeyword ();
Skw1.getvariable1 ();
// Non-static method + static variable. Before using non-static method, you need to convert it into a class object.
Skw1.getstaticvariabel ();
// Static method + static variable. Before using a non-static method, you do not need to create a class object. You can directly pass the class name in class program. static Method Name) statickeyword. country call
Statickeyword. getstaticvariable ();
}
}
}
Class Page code:

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace statickeyword
{
Class statickeyword
{
// Non-static variables
Public string variable = ": non-static variables ";
// Static variables
Public static string staticvariable = ": static variable ";
// Non-static method + non-static variable
Public void getvariable1 ()
{
Console. writeline ("this variable is" + variable );
}
// Static method + non-static variable
// Public static void getvarialble ()
//{
// Console. writeline ("this variable is" + variable); // error: non-static fields, methods, or attributes "statickeyword. statickeyword. Variable" require Object Reference
//}
// Non-static method + static variable
Public void getstaticvariabel ()
{
Console. writeline ("this variable is" + staticvariable );
}
// Static method + static variable
Public static void getstaticvariable ()
{
Console. writeline ("this variable is" + staticvariable );
}
}
}

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.