Are static variables thread-safe? C#

后端 未结 7 1610
南笙
南笙 2020-12-03 00:35

I want to create a class which stores DataTables, this will prevent my application to import a list of details each time I want to retrieve it. Therefore this should be done

7条回答
  •  [愿得一人]
    2020-12-03 01:27

    Static variables aren't thread safe per-se. You should design with thread safety in mind.

    There's a good link to get you started: http://en.csharp-online.net/Singleton_design_pattern%3A_Thread-safe_Singleton

    Apart from this, I would strongly recommend you to use a more modern approach than the legacy DataTable. Check out the Entity Framework or NHibernate. Implementing them in your datalayer will allow you to hide database details from the rest of the software and let it work on a higher level abstraction (POCO objects).

提交回复
热议问题