Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - The difference between static members and non-static members
The difference between static members and non-static members
Data members can be divided into static variables and non-static variables.

Static members: Members in static classes are added with static modifiers, that is, static members. By using class name+static member name, you can directly access this static member. Because static members exist in memory, non-static members need to be instantiated to allocate memory, so static members cannot access non-static members ... Because static members exist in memory, non-static members can directly access static members in the class.

Non-static members: all members that are not static are non-static members. After classes are instantiated, they can be accessed through the instantiated class name ... The lifetime of non-static members depends on the lifetime of the class ... and static members have no concept of lifetime, because static members always exist in the content. ..

A class can also contain static members and non-static members, and a class can also contain static constructors and non-static constructors. ..

For winApp, static members save a lot of things for programmers themselves, and because static members reside in memory and pass data between methods, static members have become my first choice ... but don't use them a lot because of convenience, especially when memory is tight or

Be careful when using static methods to manipulate some * * * enjoyment values, or when writing multi-user systems. For example:

Static int id = 0;;

SQL = " select * from table where id = "+id;

If you write this way, there will be no problem with single-machine testing, but there will be problems with multiple people testing data at the same time. If user A visits his is 20, the value of id in memory is 20, while user B visits his is 30, the value of id in memory is 30 ... If you write this method with non-static members now, this will not happen ... Because non-static members are declared by you, memory will be allocated when instantiating ... So when users visit, App will allocate memory for the user's request because of instantiation ... and when user B accesses it, it will also allocate memory because of user B's access ... so these two users access different memory blocks ... so there will be no data overlay and confusion. ...

I think this situation should explain the difference between static variables and non-static members. ..

Compared with webApp, there are far fewer factors to consider when using static under winApp than under webApp. Because webApp is a multi-user system, you should be more careful when using static. ..

And I have a question about using static under webApp, if a static method, such as:

Static string aa (string str) (

//After a series of operations ..

Returns a string;

}

Or a static method that returns a dataset.

Static data set aa (string str) (

//After a series of operations ..

Return the data set;

}

At this time, the number of visits is more, and the program is concurrent. Will it be disordered? I used a lot of static methods in public function classes used in previous projects, but fortunately, I didn't visit many times, and there was no problem all the time ... Before posting this article, I searched MSDN and CSDN, and searched some articles about static members, but there was no clear explanation ... Although I tested them in the project for so long, there was no problem ... I always felt that this might happen. ..

I wonder if you have encountered similar doubts in the project? Please give advice from friends who have had experience in this field. ..

Answer:

Whether it is abused or not, if you have conflicts, you don't understand the difference between static member variables and static methods. The static method itself is just a piece of code, no matter how it is called, there will be no problem. However, static member variables can't. They are shared by all users. If one user changes them, it will definitely affect others. This is often called concurrency conflict problem. Generally speaking, you should lock shared member variables when modifying them!

Some misunderstandings about static method and example method.

First, the static method is memory resident, and the instance method is not resident, so the static method is efficient but takes up memory.

In fact, the method is the same. Static methods and instance methods are the same in loading time and memory occupation. They are loaded the first time the type is used. There is basically no difference in call speed.

Second, static methods allocate memory on the heap, and instance methods are on the stack.

In fact, it is impossible for all methods to allocate memory on the heap or stack. As code, the method is loaded into a special code memory area, which is not writable.

Third, the instance method needs to create an instance to call, which is more troublesome, and it is simpler without using static methods.

In fact, if a method has nothing to do with an instance of its type, it should be static and no one will write it as an instance method. Therefore, all instance methods are related to instances. Since they are related to instances, creating instances is a necessary step. Simply put, there is no trouble. In fact, you can write all instance methods as static and pass in instances as parameters.

Some methods seem to have nothing to do with instances, such as IComparer. Compare method, but in fact, each class that implements this interface is only responsible for the comparison of its own type instances, which is a historical legacy caused by the lack of generics in C# 1.x specification.

Most static methods are related to instances of classes, such as various parsing methods. The reason why he makes them static is that he has no instances as parameters. Others are mostly for semantic or other purposes.