Static in C #

Source: Internet
Author: User

Static keyword Everyone is sure to use a lot of, said everyone also know, what is there to talk about?

But back to some of the previous things, inevitably call my heart ...

The cause is that some of the classes in a project are all methods, but are designed as non-static classes. So I was trying to persuade my colleagues to design these classes as static and to make a great effort. I was exhausted when my colleagues understood it.

Don't know if you have the following ideas?

    1. Static is from the process-oriented to object-oriented legacy, the past development is process-oriented, so many use static classes, static methods, to object-oriented, the keyword is preserved. Therefore, it is best not to use the static keyword in object-oriented development.
    2. Whether it's a static class or a non-static class, the effect is the same, no difference.
    3. If you have to let me use static classes, what is the point of doing this?
    4. If I put the non-static class as a static class, then what's the problem?

Below for some of the above views, the author elaborated his understanding, if there is no place to write the wrong, please also point out.

      1. The static keyword is indeed a keyword for the era of process development, but it does not mean that the static keyword should be avoided as much as possible in object-oriented development.
      2. Static class and non-static class effect is certainly not the same, there must be some difference.
      3. Based on the author's experience, the static class is generally used for classes that are not state-independent. So, what is a state-independent class? My understanding is that when there are no attributes in a class, only the method can be considered a class that is not state-independent. Recall that a non-static class instantiates an object, where is the purpose? The purpose is to save the state of the class through this instantiated object.
        Isn't it? For example
        A = new A ();
        A.name = "Kevin";
        A.sex = "male";
        ......
        The state of the class is persisted by object A through the object a that we instantiate.
        If our class has no attributes and is all methods, then is it necessary to instantiate an object in order to invoke the method in the class?
        Therefore, I suggest that, in the design of the class, if the class is a state-independent class, it is recommended that it be designed as a static class.
        As for the meaning of this: first, the performance loss of constructing an object is omitted, although it can be neglected. Second, the programmer is more friendly, the method of invoking the class is more convenient.
      4. As long as a class is a class independent of the State class, it is no problem to design it as a static class.
        For some of these views, it is purely personal experience. If you have a different point of view, welcome to communicate together.

Static in C #

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.