C# Error CS8374 – Cannot ref-assign ‘{1}’ to ‘{0}’ because ‘{1}’ has a narrower escape scope than ‘{0}’.

C# Error

CS8374 – Cannot ref-assign ‘{1}’ to ‘{0}’ because ‘{1}’ has a narrower escape scope than ‘{0}’.

Reason for the Error & Solution

Cannot ref-assign to because has a narrower escape scope than.

Example

The following sample generates CS8374 because the reference contained in r1 is externally visible but the scope of rx‘s value (its lifetime) is local:

// CS8374.cs (6,9)

class C
{
    void M(ref int r1)
    {
        int x = 0;
        ref int rx = ref x;
        for (int i = 0; i < (r1 = ref rx); i++)
        {
        }
    }
}

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...