当前位置:网站首页>Comparison of static methods and variables with instance methods and variables

Comparison of static methods and variables with instance methods and variables

2022-06-26 03:44:00 yuyue5945

C# Commonly used Static Modify static variables and static methods

What is the difference between static variables and instance variables ?

  • 1、 Grammatically : Static variables are preceded by Stati keyword , Instance variables do not require .
  • 2、 Runtime : When the program runs, the static variables will be allocated space , The instance variable belongs to the attribute of an object , You need to create an instance object to allocate space .
  • 3、 Storage area : Static variables are stored in static storage , Ordinary variables are stored in the heap .

There are two main differences between static method and instance method :

  • When a static method is called externally , have access to " Class name . Method name " The way , You can also use " Object name . Method name " The way . And the instance method only has the latter way . in other words , You can call static methods without creating objects .

  • Static methods when accessing members of this class , Only static members are allowed ( Static member variables and static methods ), Access to instance member variables and instance methods is not allowed ; There is no limit for instance methods .

Thread safety of static methods

Let's briefly discuss the use of static fields in a class (static field) And static methods (static method) Whether there will be thread safety problems .

We're learning , Static field (static field) And static methods (static method) Is called through a class . Static methods do not operate on specific instances , Only static members can be accessed . Instance methods can operate on specific instances , Can access static members , Can also access instance members .

that , Whether using static methods in multithreading has thread safety problems ? This depends on whether static methods cause thread safety problems and whether static members are used in static methods .

because , When using the same static method in multiple threads , Each thread uses its own instance field (instance field) Copy of , And share a static field (static field). So , If the static method does not operate on a static member , Use instance fields only inside methods (instance field), Will not cause security problems . however , If the static method operates on a static field , You need to use mutually exclusive access in static methods for security processing .

A simple example , What we use Console.WriteLine(); in WriteLine() yes Console.WriteLine Class static methods .

If static methods do not use static variables , There are no thread safety issues .
Why? ? Because variables declared in static methods , When each thread calls , Will create a new , Instead of sharing a storage unit . Like here tmp, Each thread creates its own , So there's no thread safety issue .
Be careful : Static variables , Because it occupies a storage area when the class is loaded , Each thread shares this store , So if you use static variables in static methods , There will be thread safety issues !

Blogger GitHub Address

https://github.com/yuyue5945

Pay attention to the official account

 official account

原网站

版权声明
本文为[yuyue5945]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180546224884.html