C# Error CS1673 – Anonymous methods, lambda expressions, query expressions, and local functions inside structs cannot access instance members of ‘this’. Consider copying ‘this’ to a local variable outside the anonymous method, lambda expression, query expression, or local function and using the local instead.

C# Error

CS1673 – Anonymous methods, lambda expressions, query expressions, and local functions inside structs cannot access instance members of ‘this’. Consider copying ‘this’ to a local variable outside the anonymous method, lambda expression, query expression, or local function and using the local instead.

Reason for the Error & Solution

Anonymous methods, lambda expressions, and query expressions inside structs cannot access instance members of ‘this’. Consider copying ‘this’ to a local variable outside the anonymous method, lambda expression or query expression and using the local instead.

The following sample generates CS1673:

// CS1673.cs  
delegate int MyDelegate();  
  
public struct S  
{  
   int member;  
  
   public int F(int i)  
   {  
       member = i;  
       // Try assigning to a local variable  
       // S s = this;  
       MyDelegate d = delegate()  
       {  
          i = this.member;  // CS1673  
          // And use the local variable instead of "this"  
          // i =  s.member;  
          return i;  
  
       };  
       return d();  
   }  
}  
  
class CMain  
{  
   public static void Main()  
   {  
   }  
}  

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...