C # static class and Static function __java

Source: Internet
Author: User
Tags instance method static class

Learn about static classes and static functions.
The content of this blog is from MSDN, and of course I read the MSDN translation myself. Body

Static and Non-static classes are essentially the same. There is one difference: Static classes cannot be instantiated. In other words, you can't get new to an object for a static class instance. Because there are no instance members in the static class at all. You can access static class variables only by using the class name. For example: If your static class is called Utilityclass, and there is a method in this class called MethodA, you can only invoke it as follows.

Utilityclass.methoda ();
Static classes can be used as a convenient container for storing a set of functions that are operated only by input parameters, without the need to read and write the instance feild within the class. For example, in the. NET class Library The methods contained in the System.Math static class perform only mathematical calculations without the need to store and retrieve data specific to the Math class instance, that is, you need to use class members by specifying the class name and method name, as shown in the following example.

As with all class types, when a program referencing a static class is loaded, the type information of the static class is loaded through the common language runtime. The program does not specify exactly when the class is loaded. However, the CLR guarantees that the class is loaded before the static class is first referenced, initializes the field of the class, and invokes its constructor.

The following contains the main features of the static class
Contains only static members
Cannot be instantiated
Sealed closed.
Cannot contain instance constructors

Therefore, creating a static class and creating a class that contains only static members and private constructors is basically the same. Private constructor method class instantiation. The advantage of using static classes is that the compiler can check to make sure that no accidental addition of instance members is possible. The compiler ensures that objects cannot be created.

Static classes are closed and therefore cannot be inherited. In addition to the Objecet class, it cannot inherit from other classes. A static class cannot contain an instance constructor. However, you can include static constructors. Non-static classes should also define static constructors if Non-static classes require initialization of important static members.

Static members
A non-static class can contain static functions, fields, properties, and methods.

Non-static classes can contain static methods, fields, properties, events. Static members can be invoked even when an instance of a class is not created. Static members are accessed through the class name, not the object name. No matter how many objects are created, there is only one copy of the static member. Static methods and properties do not have access to Non-static fields, event xx, and they cannot access an instance of any object (unless it displays the pass in the method parameter). Declaring a non-static class that contains static members is more typical than declaring a complete static class.

To jointly maintain a static field that computes the instantiated class, or to store a value that must be shared to all objects.
Static methods can be overloaded, but cannot be overridden, because they belong to a class and do not belong to any instance.
Although the field cannot be declared static const, a const field is static in nature from a static behavior. It belongs to a static type, not an instance type. Therefore, the Const field can also be accessed by using Classname.membername.

C # does not support static local variables (variables declared in the scope of the method).

Before a static member is accessed for the first time, a static constructor is instantiated if one of the static members is invoked.
To access a static class member, use the class name instead of a variable name
, if a class contains static fields, you can write a static constructor that, when the class loads, constructors can initialize them.

A call to a static member produced a call instruction in MSIL, and the invocation of an instance method produced a callvir instruction.
However, most of the time the difference between the two is not so important.

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.