none
Which assembly and namespace contains the definition for lock. RRS feed

  • Question

  •         static void Main(string[] args)
            {
                object o = new object();
                lock (o)
                {
                }
            }


    I tried to get the definition(source code ) of the lock by clicking "go to definition", however failed with above error,so may I known which assembly and namespace contains the definition for lock.?


    • Edited by Jacky_shen Monday, May 9, 2016 2:19 PM
    Monday, May 9, 2016 2:17 PM

Answers

  • "If this translation done by c# compiler ,which functions\methods implement it from the source code of the c# compiler?"

    In the case of Roslyn the relevant code can be found here.

    • Marked as answer by Jacky_shen Tuesday, May 10, 2016 9:44 AM
    Tuesday, May 10, 2016 9:07 AM
    Moderator

All replies

  •         static void Main(string[] args)
            {
                object o = new object();
                lock (o)
                {
                }
            }


    I tried to get the definition(source code ) of the lock by clicking "go to definition", however failed with above error,so may I known which assembly and namespace contains the definition for lock.?


    "lock" is a keyword in the C# language.

    It's "syntactic sugar" for calls to Monitor.Enter and Monitor.Exit, see here for more, it basically manages Win32 Critical Sections under the hood.


    Monday, May 9, 2016 7:08 PM
  •         static void Main(string[] args)
            {
                object o = new object();
                lock (o)
                {
                }
            }


    I tried to get the definition(source code ) of the lock by clicking "go to definition", however failed with above error,so may I known which assembly and namespace contains the definition for lock.?


    "lock" is a keyword in the C# language.

    It's "syntactic sugar" for calls to Monitor.Enter and Monitor.Exit, see here for more, it basically manages Win32 Critical Sections under the hood.


    Hi Korporal,

    Thank you for your help.

    In fact I know the lock would be call for Monitor.Enter and Monitor.Exit from some articles, however May I know how to prove it? The C# compiler would translate the lock to Monitor.Enter and Monitor.Exit when compiling the program or else?

    Tuesday, May 10, 2016 4:00 AM
  • "In fact I know the lock would be call for Monitor.Enter and Monitor.Exit from some articles"

    If you're interesting in this kind of stuff then you'd better read the relevant specifications - C# spec, ECMA 335 spec etc. instead of reading articles or asking questions in forums. In this particular case the answer is trivial to find in the C# spec and is as accurate as it can be:

    8.12 The lock statement

    A lock statement of the form
    lock (x) ...
    where x is an expression of a reference-type, is precisely equivalent to
    bool __lockWasTaken = false;
    try {
     System.Threading.Monitor.Enter(x, ref __lockWasTaken);
     ...
    }
    finally {
     if (__lockWasTaken) System.Threading.Monitor.Exit(x);
    }
    except that x is only evaluated once.

    Tuesday, May 10, 2016 4:56 AM
    Moderator
  • "In fact I know the lock would be call for Monitor.Enter and Monitor.Exit from some articles"

    If you're interesting in this kind of stuff then you'd better read the relevant specifications - C# spec, ECMA 335 spec etc. instead of reading articles or asking questions in forums. In this particular case the answer is trivial to find in the C# spec and is as accurate as it can be:

    8.12 The lock statement

    A lock statement of the form
    lock (x) ...
    where x is an expression of a reference-type, is precisely equivalent to
    bool __lockWasTaken = false;
    try {
     System.Threading.Monitor.Enter(x, ref __lockWasTaken);
     ...
    }
    finally {
     if (__lockWasTaken) System.Threading.Monitor.Exit(x);
    }
    except that x is only evaluated once.

    Mike,

    Thank you again for your kindly help.

    If I can change my question , maybe I want to ask how the C# spec for translating the lock to monitor.enter and monitor.exit is implemented from the code perspective.   If this translation done by c# compiler ,which functions\methods implement it from the source code of the c# compiler?

    Tuesday, May 10, 2016 9:03 AM
  • "If this translation done by c# compiler ,which functions\methods implement it from the source code of the c# compiler?"

    In the case of Roslyn the relevant code can be found here.

    • Marked as answer by Jacky_shen Tuesday, May 10, 2016 9:44 AM
    Tuesday, May 10, 2016 9:07 AM
    Moderator
  • "If this translation done by c# compiler ,which functions\methods implement it from the source code of the c# compiler?"

    In the case of Roslyn the relevant code can be found here.

    This is exactly what I needed!!!

    Thank you so much for your kindly Help!

    Tuesday, May 10, 2016 9:44 AM