
include::../description-dotnet.adoc[]

=== Code examples

==== Noncompliant code example

[source,csharp,diff-id=1,diff-type=noncompliant]
----
public class Example
{
    private static ReaderWriterLock rwLock = new();

    public void Writer()
    {
        rwLock.AcquireWriterLock(2000);
        try
        {
            // ...
        }
        finally
        {
            rwLock.ReleaseReaderLock(); // Noncompliant, will throw runtime exception
        }
    }

    public void Reader()
    {
        rwLock.AcquireReaderLock(2000);
        try
        {
            // ...
        }
        finally
        {
            rwLock.ReleaseWriterLock(); // Noncompliant, will throw runtime exception
        }
    }
}
----

==== Compliant solution

[source,csharp,diff-id=1,diff-type=compliant]
----
public class Example
{
    private static ReaderWriterLock rwLock = new();

    public static void Writer()
    {
        rwLock.AcquireWriterLock(2000);
        try
        {
            // ...
        }
        finally
        {
            rwLock.ReleaseWriterLock();
        }
    }

    public static void Reader()
    {
        rwLock.AcquireReaderLock(2000);
        try
        {
            // ...
        }
        finally
        {
            rwLock.ReleaseReaderLock();
        }
    }
}
----

include::../resources-dotnet.adoc[]

include::../rspecator.adoc[]