Why doesn't C# allow a null value to be locked?

前端 未结 5 447
不知归路
不知归路 2021-01-17 09:27

C# doesn\'t allow locking on a null value. I suppose I could check whether the value is null or not before I lock it, but because I haven\'t locked it another thread could c

5条回答
  •  走了就别回头了
    2021-01-17 09:58

    You cannot lock on a null value because the CLR has no place to attach the SyncBlock to, which is what allows the CLR to synchronize access to arbitrary objects via Monitor.Enter/Exit (which is what lock uses internally)

提交回复
热议问题