C # The little things about static and non-static

Source: Internet
Author: User

The so-called static and non-static, it is in the declaration of a more use of a "static."

So what is the difference between static and non-static?

1, for non-static members, including methods, fields, properties. Called when it needs to be invoked with an instantiated object. (new comes out.) )

For static members, calls are called using the class name.

This gives a question, can you use static classes to instantiate objects? The answer is, of course, negative, because the purpose of instantiating an object is to invoke an attribute, method, or field with an instantiated object. For a static class, it is called with the class name, so there is no need to instantiate an object for a static class. It is not possible to declare a static class object in VS.

2. In non-static functions, both static and non-static members can be used.

In static functions, only static methods, fields, and properties can be used.

3. In a static class, only static members are allowed, and instance members are not allowed to appear. (The so-called instance member, refers to the non-static member)

In non-static classes, you can allow static members to appear, and you can allow instance members to appear.

This brings up the problem that most of the classes are static classes. For example, we often use, Console.WriteLine (), Console.ReadLine () and so on. They are static classes that are called with the class name.

4. For non-static classes, members can be instantiated. Therefore, the class does not occupy memory space, and the instantiated object is space-occupying.

For static classes, classes are space-occupying. Because the memory space is differentiated as follows:

Therefore, the static class in the program is occupied space, there is a static storage area.

As a result, static classes are shared resources throughout the program project. Also, static classes are freed only after the program has finished.

The above is a small summary of static and non-static classes.

Reprint Please specify blog garden: http://www.cnblogs.com/gu-zhan/old Salty

C # The little things about static and non-static

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.