About static in c #

Source: Internet
Author: User

There must be a lot of static keywords, and everyone knows about them. What is the point of discussion?

But when I think back to some previous things, I can't help thinking about them ......

The cause is that some classes in a project are all methods, but they are designed as non-static classes. So I tried a lot to persuade my colleagues to design these classes as static ones. After my colleagues understood it, I was exhausted.

Do you have the following ideas?

  1. Static is a legacy product from process-oriented to object-oriented. In the past, development was process-oriented, so many static classes and static methods were used. After the process-oriented approach, this keyword is retained. Therefore, it is recommended that you do not need or use less static keywords in object-oriented development.
  2. Whether it is a static or non-static class, the effects are the same and there is no difference.
  3. If you want me to use static classes, what does this mean?
  4. If I use a non-static class as a static class, will there be any problems?

The following are some of the above points of view. I will explain my understanding. If there is something wrong with writing, please also point out.

  1. Static keywords are indeed some keywords in the era of process-oriented development, but they do not mean that static keywords should be avoided as much as possible during object-oriented development.
  2. Static classes and non-static classes certainly have different effects.
  3. Based on my experience, static classes are generally used for classes unrelated to States. So what is a class unrelated to the status? In my understanding, when there is no attribute in a class and there is only a method, we can think that this class is state-independent. Recall that a non-static class instantiates an object. Where is the purpose? The purpose is to save the class State through this instantiated object.
    Isn't it? For example
    A a = new ();
    A. Name = "Kevin ";
    A. Sex = "male ";
    ......
    By instantiating object a, the class state is maintained by object.
    If our class has no attributes and is full of methods, is it necessary to instantiate an object first to call methods in the class?
    Therefore, the author suggests that when designing a class, if the class is a class unrelated to the State, it is recommended to design it as a static class.
    As for the significance of doing so: first, the performance loss of constructing an object is omitted, although negligible. Secondly, it is more friendly to programmers and more convenient to call class methods.
  4. As long as a class is not related to the status class, it is no problem to design it as a static class.
    The above points are purely personal experiences. If you have different opinions, please share them with us.

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.