在锁内返回会有什么不同吗?

时间:2010-07-16 20:50:21

标签: .net locking

样本一

Public _objLock As Object = New Object
Public ReadOnly Property MyObjects() As IEnumerable(Of Object)
    Get
        SyncLock _objLock
            If _myObjects Is Nothing Then
                _myObject = LoadMyObjects()
            End If
            Return _myObjects
        End SyncLock
    End Get
End Property

样本二

Public _objLock As Object = New Object
Public ReadOnly Property MyObjects() As IEnumerable(Of Object)
    Get
        SyncLock _objLock
            If _myObjects Is Nothing Then
                _myObject = LoadMyObjects()
            End If              
        End SyncLock
        Return _myObjects
    End Get
End Property

这些实现之间会有什么区别吗?

1 个答案:

答案 0 :(得分:5)

不,在锁内返回没有任何区别。一旦你离开锁,它将不复存在。