The role of static variables and static methods in C #

Source: Internet
Author: User

1. static variables

In a C # program, there is no concept of a global variable, which means that all member variables can manipulate the data only if it is an instance of that class, which plays a role in "information hiding." But sometimes it's not a wise choice.

Suppose we want to define a book class that requires the class to hold the number of books, that is, each additional book (defining an instance), the number of books should be added by 1. If there is no static variable, we need to keep the number of books in each book (instance), however, such variables to be stored in different books (instances), the number of books (instances), we can accept, if the number of books (examples), such as thousands, We can't imagine how much resources (such as memory, disk space) can be wasted, and what's even more unbearable: because the number of books (instances) is kept in every book (instance), the value is definitely different. To match the number of books (instances) saved in these books (instances), we must modify the values that are saved in all other books (instances) when each additional new book is generated (creating a new instance). Oh,my god! you will re-aspire to process-oriented program design approach, yearning to have global variables of the era. However, this situation does not occur because you have another variable type in C #: a static variable. It is like a global variable in a class that holds the public information for a class, and all instances (objects) of that class share the value.

Static variables are declared in the following way:

[Access modifier] static data type variable name;

The access modifier here, like other members of the class, can be public,protected,private or internal, and so on.

And how do static variables work?

Static variables must be referenced using the class name, not an instance of the class, because static variables are not part of any instance, but are common. We can make an analogy: in a class, some items are personal, we want to use, we must point out the owner of the item, such as "Wang San Bike", in C # program we can use: Wang San. bike format. Some items are common items, can not be used in the name of the individual, but used in the name of the class, such as the collective contribution to buy basketball, can only say: "Class basketball", and can not say: "Wang San basketball." This is absolutely not possible, it is absolutely unfair to others, we can think of many corrupt officials are using things that do not belong to themselves, or in the personal name of the use of public things and ruined themselves.

A useful thing to say is that a static variable refers to it by using the class name. That is: class name. static variable name;

Here is a concrete example:

Using System;

Class Staticvar
{
public int x;
public static int y;
public void Printinfo ()
{
Console.WriteLine ("Non-static variable x={0}", x);
Console.WriteLine ("Static variable y = {0}", y);
}
}
Class Test
{
static void Main (string[] args)
{
Staticvar STV = new Staticvar ();
stv.x = 10;
Stv.y = 20; Error, unable to access static member "STATICVAR.Y" using instance reference, and use type name to qualify it
Staticvar.y = 20;
Stv. Printinfo ();
}
}

Part of the program that is commented on: Stv.y = 20 is using an instance to refer to a static variable, which throws an error.

In addition, I would like to say that for static variables when declaring, if not given the initial value or not assigned any value before use, the system will give them a default value: For the integer data default is 0, the single-precision data is: 0.0f, double precision data is 0.0, the Boolean data is false, the reference data is null.

2. Static methods

Static methods, like static variables, do not belong to any particular instance, and are common to all members of the class and are called by the class name. But pay attention to the following points:

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. Here is a concrete example to illustrate:

Using System;

Namespace Teststatic
{
Class Statictest
{
int x;
static int y;
Public statictest (int a,int b)
{
x = A;
y = b;
}
public void Simpleprint ()
{
Console.WriteLine ("X=" +x+ ", y=" +y);
}
public static void Staticprint ()
{
Console.WriteLine ("Y={0}", y);
Console.WriteLine ("X={0}", x); Non-static members cannot be used in static methods
}
}
Class Test
{
static void Main (string[] args)
{
Statictest st = new Statictest (10,23);
St. Simpleprint ();
St. Staticprint (); Static methods cannot use instances to invoke
Statictest.staticprint ();
}
}
}

The role of static variables and static methods in C #

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.