How to use the C static keyword, what phenomena and problems will there be
Old dream of Nancheng 2020-11-26 06:00:59
This article is suitable for experienced Middle and advanced programmers read .

this paper Not for beginners About static How to use the keyword tutorial style article , But mainly focuses on what will happen after using it , It belongs to the experience after practice .

Limited to my limited level and experience , What you write is not necessarily accurate and comprehensive , Please help correct the axe .

C# static How to use keywords , What are the phenomena and problems


One 、 Let's talk about the tutorial

Maybe it's necessary to say the key words briefly static course , Use static Modifiers can declare static members that belong to the type itself, not to a specific object . static Modifiers can be used to declare static class . In the class 、 In interfaces and structures , Can be static Modifier added to field 、 Method 、 attribute 、 Operator 、 Events and constructors . Here's the picture :

C# static How to use keywords , What are the phenomena and problems


C# static How to use keywords , What are the phenomena and problems


Two 、 Static methods reside in memory , So it's efficient , Fast , But the memory will be higher

This view is wrong , For the same class , It has the same memory overhead , Efficiency is not as fast as that , Because it's addressing .

3、 ... and 、 Declaration of a class

Once a class is declared , It will create a new copy of all instance fields of the class , but static Fields will only have one copy .

meanwhile , An instance of a class can be passed through this Object access , but static Can not be .

Four 、 Use static Variables can cause problems

because static Field , Once it is declared loaded into memory , It's always been there , And there's only one copy , therefore , Any changes you make to this field , Will be reflected throughout the life cycle .

That means , Use static Field , To prevent values from being changed , Unless you do it on purpose .

For this feature ,static It can be used in runtime design (Runtime). such , You can have global , Public data that can be used anywhere .

5、 ... and 、 Novice programmers , Why? General All are static

For modern programmers , Usually refers to 2012 The programmer of the year after , They may be due to the fact that when they go into programming , It's quite modern , They didn't know that early computers came along , And what programming looked like at the beginning .

I don't know , I am a 2000 Years later, contact programming , about 70-80 At that time, the level of programmers in those days can only be told by hearsay .

At the beginning of programming , All procedural theories are constructed in the West , Or the beginning of chaos , A lot of things are still groping , Like object-oriented 、 Design patterns 、 Various underlying designs 、 Various programming languages 、 Basic algorithm design and so on , All need to form and develop . And modern programmers , It was to enjoy the fruits of people's thoughts at that time .

In the early days , Western programmers back then , It's just the whole story static, Why? ? Because at that time, the theoretical knowledge of object-oriented was not fully formed , Everything is exploring , in addition , These Western programmers , It's like the programmers in the kingdom of God , young , They are all inexperienced beginners , What kind of code can a novice write , You know .

So the programmer of the kingdom of God , Why are they so modern now , Or use the whole article from the beginning of chaos static Well ? The answer may be convenient , And the process orientation that comes with your mother is born with . I once heard a programmer interview , He said that what he studied and studied was process oriented , I was happy , Does process orientation require you to learn and Research , You are born to .

6、 ... and 、 With static keyword , It's so convenient again , Why instantiate objects ?

This problem may be a problem that many people have not thought about and thought about . Yeah , Memory is the same , The speed is the same . Why can't I go through it static, have to new An object ?

The answer is : object-oriented .

With the image of various knowledge theories , Due to the good robustness of object-oriented extension, maintainability and other reasons , He can develop an extremely convenient and constantly upgraded system , For social progress has a strong role in promoting , therefore , That's what you need to do new once , Instead of using it all the time static Why .

imagine , You always use static To do something , image C#、Java Language . You're going to upgrade to C#9.0 edition ,Java N.0 edition , You're all about it static How to expand, how to maintain, how to upgrade , How to promote social development ?

7、 ... and 、 When to use static

We know , All the classes , If you will , All methods can be changed to static, Just pass in the parameters . all static It can also be converted to instance method , Remove static.

Um. , Yes , When to use static Well ? What's the principle ?

I offer some ideas based on personal experience , Maybe not enough , Please point out and correct :

1. Use the example method , You can make your code object-oriented , So it's very beautiful . I won't talk about the advantages of object orientation .

2. When you need to access a method easily , May adopt static. The convenience depends on you .

3. When you are limited by the system , You have to use static when , You have no choice , You have to use . For example, object extension methods .

4. When you want to use Fluent Method to quickly operate a class , May adopt static The way .

5. You have special design requirements , For example, the runtime design of the lifecycle .

Have a good meal .
版权声明
本文为[Old dream of Nancheng]所创,转载请带上原文链接,感谢
https://fheadline.com/2020/11/20201126055922394g.html
相似文章

2021-08-09