简体   繁体   中英

Scope of static class constructor and static class fields

I have 2 Console Applications (Console1 and Console2) in one solution. Both applications reference a class library (CL). CL contains a static class (SC) which contains fields that are set per constructor.

My question is if I call the static class within Console1 and the constructor of the SC is called upon first calling any of Console1's containing static methods and fields are set within SC. Now if I run Console2 concurrently, though independently, and also access the SC, what is the precise scope of the static class SC? Will any changes such as calling its constructor that originated from within Console1 have any bearing whatsoever on how the SC behaves when being used in Console2?

The scope is the AppDomain.

Because you have two separate processes, you have two separate AppDomains and two separate "instances" of the static class. There is no state sharing - none at all.

如果Console1和Console2像两个完全独立的exe文件一样独立运行,则将再次调用SC构造函数,否则不会调用。

Console 1 and 2 are running as separate processes. The assemblies are loaded per process (per app domain to be precise), and each will initialize the static constructor and such. Therefore the 'running instances of the assemblies' are completely separate.

Your question may be about static classes but the answers you are getting will all boil down to this: If you run two separate instances of a project the classes used in one instance will not interact with the other. In fact you could run two instances of the same project and you will have no problems. It is no different to running two instances of Microsoft word

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM